Welcome to WebmasterWorld Guest from 54.226.133.245

Forum Moderators: open

Message Too Old, No Replies

Open extendSearch link in New Window

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

Junior Member

5+ Year Member

joined:Feb 21, 2006
posts: 54
votes: 0


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)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Nov 3, 2005
posts:1585
votes: 0


Try

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

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

Junior Member

5+ Year Member

joined:Feb 21, 2006
posts: 54
votes: 0


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)

Senior Member from US 

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

joined:Oct 17, 2005
posts:4966
votes: 10


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)

Senior Member from US 

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

joined:Oct 17, 2005
posts:4966
votes: 10


<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)

Junior Member

5+ Year Member

joined:Feb 21, 2006
posts: 54
votes: 0


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