944,020 Members | Top Members by Rank

Ad:
Feb 14th, 2007
0

new window? target="_blank" ?

Expand Post »
I want random links in this code to open in a new window. how to please ?

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <script language="JavaScript">
  2. <!--
  3. // Create arrays to contain all the values
  4. // for links and image locations
  5. link = new Array
  6. image = new Array
  7.  
  8. link[1]="http://www.wildexpo.com.au/Aust%20Rep%20Show/"
  9. image[1]="http://www.reptilesaustralia.com.au/forums/Ads/wildexpo.gif"
  10.  
  11. link[2]="http://www.excite.com"
  12. image[2]="http://www.excite.com/mesp/images/excite/new_logo-180.gif"
  13.  
  14. link[3]="http://www.lycos.com"
  15. image[3]="http://a284.g.akamai.net/7/284/987/000/lygo.com/ly/i/lyguide.gif"
  16.  
  17. link[4]="http://www.cnet.com"
  18. image[4]="http://cnet.com/Images/Headers/cnet-1-title.gif"
  19.  
  20. link[5]="http://www.webcrawler.com"
  21. image[5]="http://webcrawler.com/img/web/hdr/home_header.gif"
  22.  
  23.  
  24. // Create a random number between 1 and five
  25. random_num = (Math.round((Math.random()*4)+1))
  26.  
  27.  
  28. // Write a link and images with random array
  29. document.write("<a href=\"" + link[random_num] + "\">");
  30. document.write("<img src=\"" + image[random_num] + "\" border=\"0\"></a>");
  31.  
  32. -->
  33. </script>
Similar Threads
Reputation Points: 11
Solved Threads: 6
Posting Whiz in Training
Inny is offline Offline
293 posts
since Oct 2005
Feb 14th, 2007
0

Re: new window? target="_blank" ?

target = "_blank" should work.

change:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. document.write("<a href=\"" + link[random_num] + "\">");
to
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. document.write("<a href=\"" + link[random_num] + "\" target=\"_blank\">");
Moderator
Featured Poster
Reputation Points: 522
Solved Threads: 64
Veteran Poster
MattEvans is offline Offline
1,091 posts
since Jul 2006
Feb 14th, 2007
0

Re: new window? target="_blank" ?

Thanks Again Matt!

Resolved!
Reputation Points: 11
Solved Threads: 6
Posting Whiz in Training
Inny is offline Offline
293 posts
since Oct 2005
Feb 14th, 2007
0

Re: new window? target="_blank" ?

Not Quite Resolved! Whats wrong with back slash? The code works in testbed but wont cop the backslash on my board (ipb) ?

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. document.write("<a href=\"" + link[random_num] + "\" target=\"_blank\">");
  2.  
  3. document.write("<img src=\"" + image[random_num] + "\" border=\"0\"></a>");
Reputation Points: 11
Solved Threads: 6
Posting Whiz in Training
Inny is offline Offline
293 posts
since Oct 2005
Feb 14th, 2007
0

Re: new window? target="_blank" ?

Click to Expand / Collapse  Quote originally posted by Inny ...
Not Quite Resolved! Whats wrong with back slash? The code works in testbed but wont cop the backslash on my board (ipb) ?

[/code]
Hmm... perhaps your board software is looking for escaped characters and eating them.. o_O

try either:

document.write("<a href=\\"" + link[random_num] + "\\" target=\\"_blank\\">");
document.write("<img src=\\"" + image[random_num] + "\\" border=\\"0\\"></a>");

(double backslash should be escaped to backslash by your board software which can then go on to be the javascript escape character when the page is rendered)

or (my prefered way):

document.write('<a href="' + link[random_num] + '" target="_blank">');
document.write('<img src="' + image[random_num] + '" border="0"></a>');

(all literal strings in the Javascript are quoted with single quote characters, and attributes in the dynamically created elements are quoted with double quote characters :- escaping doesn't matter)
Last edited by MattEvans; Feb 14th, 2007 at 2:53 pm.
Moderator
Featured Poster
Reputation Points: 522
Solved Threads: 64
Veteran Poster
MattEvans is offline Offline
1,091 posts
since Jul 2006
Feb 14th, 2007
0

Re: new window? target="_blank" ?

Matt to the resue! Once again Thankyou mate!
Reputation Points: 11
Solved Threads: 6
Posting Whiz in Training
Inny is offline Offline
293 posts
since Oct 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Center Div in Browser
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Reference to undefined property FIREFOX





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC