Welcome to WebmasterWorld Guest from 54.205.209.95

Forum Moderators: open

Message Too Old, No Replies

Open extendSearch link in New Window

   
12:52 am on Nov 13, 2008 (gmt 0)

5+ Year Member



How can I get the hyperlink below to open in a new window? Target="_blank" does not work.

<script type="text/javascript">
function extendSearch(addon) {
window.location.search += addon;
}
</script>
...
<a href="#" onclick="extendSearch('&page=2');return false">

1:46 am on Nov 13, 2008 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



Try

function extendSearch(addon) {
window.open(window.location.href + addon, '_blank' );
}

4:25 pm on Nov 13, 2008 (gmt 0)

5+ Year Member



Thanks for your help. When I tried the suggestion, the new window opened to the following address:

http://www.example.com/&print=true

How can I get the new window to open to the address below?

http://www.example.com/?&print=true

[edited by: Fotiman at 6:26 pm (utc) on Nov. 13, 2008]
[edit reason] Examplified the URLs [/edit]

6:33 pm on Nov 13, 2008 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



You'd want to first check to see if there is a ? in the current location URL. If not, then append one on, if so, then append '&' instead.


function extendSearch(addon) {
if (window.location.href.indexof('?') == -1) {
// Append ?
addon = '?' + addon;
}
else {
// Append &amp;
addon = '&amp;' + addon;
}
window.open(window.location.href + addon, '_blank');
}

Or the more condensed form:


function extendSearch(addon) {
window.open(window.location.href + (window.location.href.indexOf('?') == -1 ? '?' : '&amp;') + addon, '_blank');
}

And make sure your links don't include the &.

[edited by: Fotiman at 6:37 pm (utc) on Nov. 13, 2008]

6:38 pm on Nov 13, 2008 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



<a href="#" onclick="extendSearch('page=2');return false">

(Though it's important to note that this link will not work for any visitor, including search engines, that have JavaScript disabled)

9:42 pm on Nov 14, 2008 (gmt 0)

5+ Year Member



Thank you, Fotiman! The "condensed" version of your Javascript code worked for me!