homepage Welcome to WebmasterWorld Guest from 23.23.22.200
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe to WebmasterWorld
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Open extendSearch link in New Window
rcshield




msg:3785275
 12:52 am on Nov 13, 2008 (gmt 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">

 

daveVk




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

Try

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

rcshield




msg:3785723
 4:25 pm on Nov 13, 2008 (gmt 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]

Fotiman




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

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]

Fotiman




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

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

rcshield




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

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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved