944,171 Members | Top Members by Rank

Ad:
Oct 24th, 2006
0

Please help a newbie - Javascript basic

Expand Post »
Hi All,

I wonder if any Javascript experts can help me. I have the following code on my site which create a rotating graphic in the header every time the page loads. This works great.

However what I want to do is to change the way it works slightly so that I do not have to update the code in the future.

I would like to be able to put all the image address into a text file and then for the javascript to read them from there rather than hard codeing into the web page

or

for the javascript to read all the filenames from a directory of another server?

Is this possible and can anyone help me.

Many thank in advance.




JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. #header {
  2. background:#476 url("http://www.blogblog.com/rounders4/bg_hdr_bot.jpg") no-repeat left bottom;
  3. margin:0 0 0;
  4. padding:0 0 8px;
  5. color:#fff;
  6. }
  7.  
  8. Basically my script works by overwriting the 'background' definition at the <body> part of the template. I wrote the following right after the <body> tag:
  9.  
  10. <script type="text/javascript">
  11.  
  12. var banner= new Array()
  13.  
  14. banner[0]="http://static.flickr.com/84/269053449_acad87a793_o.jpg"
  15. banner[1]="http://static.flickr.com/95/269053438_4ef5a3983e_o.jpg"
  16. banner[2]="http://static.flickr.com/96/269053404_44a3b0eda8_o.jpg"
  17. banner[3]="http://static.flickr.com/108/269053262_608edbdda4_o.jpg"
  18. banner[4]="http://static.flickr.com/98/269053232_6f7c6f994a_o.jpg"
  19. var random=Math.round(4*Math.random());
  20.  
  21. document.write("<style>");
  22. document.write("#header {");
  23. document.write(' background:url("' + banner[random] + '") no-repeat left TOP;');
  24. document.write(" }");
  25. document.write("</style>");
  26.  
  27. </script>
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
flub is offline Offline
3 posts
since Oct 2006
Oct 24th, 2006
0

Re: Please help a newbie - Javascript basic

HI,

Yes this very Much possible you can use the XML file for store the URLs,
I this in one of my project

here is the solution:

For Example:http://www.katarey.com/forHelp/xml_javascript.html

make a XML file Name

img.xml
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <?xml version="1.0" encoding="iso-8859-1"?>
  2. <images>
  3. <img>http://static.flickr.com/84/269053449_acad87a793_o.jpg</img>
  4. <img>http://static.flickr.com/95/269053438_4ef5a3983e_o.jpg</img>
  5. <img>http://static.flickr.com/96/269053404_44a3b0eda8_o.jpg</img>
  6. <img>http://static.flickr.com/108/269053262_608edbdda4_o.jpg</img>
  7. <img>http://static.flickr.com/98/269053232_6f7c6f994a_o.jpg</img>
  8. </images>

then a html/php/asp what ever, i am using xml_javascript.html

xml_javascript.html

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <script type="text/javascript">
  2. //http://www.katarey.com
  3. var xmlDoc=new ActiveXObject("Microsoft.XMLDOM")
  4. xmlDoc.async="false"
  5. xmlDoc.load("img.xml")
  6. var banner= new Array()
  7.  
  8. nodes=xmlDoc.documentElement.childNodes
  9. banner[0]= nodes.item(0).text
  10. banner[1]= nodes.item(1).text
  11. banner[2]= nodes.item(2).text
  12. banner[3]= nodes.item(3).text
  13. banner[4]= nodes.item(4).text
  14.  
  15. var random=Math.round(4*Math.random());
  16.  
  17. document.write("<style>");
  18. document.write("#header {");
  19. document.write(' background:url("' + banner[random] + '") no-repeat left TOP; width:740px; height:185px;');
  20. document.write(" }");
  21. document.write("</style>");
  22.  
  23. //Remove this Linke after testing
  24. alert(banner[random]);
  25. //*******************************
  26.  
  27. </script>
  28. <body>
  29. <div id="header"> </div>
  30. </body>



Rahul
http://www.katarey.com
Last edited by katarey; Oct 24th, 2006 at 8:01 pm. Reason: for adding the Example
Reputation Points: 39
Solved Threads: 23
Junior Poster
katarey is offline Offline
167 posts
since Jul 2005
Oct 25th, 2006
0

Re: Please help a newbie - Javascript basic

That'll only work in Internet Explorer surely? Do Netscape have an XMLDOM object equivalent? I know you can use a XMLHTTP transaction in Windows AND Netscape... but that's possibly overkill.

http://www.jibbering.com/2002/4/httprequest.html
Moderator
Featured Poster
Reputation Points: 522
Solved Threads: 64
Veteran Poster
MattEvans is offline Offline
1,091 posts
since Jul 2006
Oct 25th, 2006
0

Re: Please help a newbie - Javascript basic

Try this:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. var xmlDoc;
  2. if (window.ActiveXObject)
  3. xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
  4. else if (document.implementation && document.implementation.createDocument)
  5. xmlDoc= document.implementation.createDocument("","doc",null);
Reputation Points: 10
Solved Threads: 0
Newbie Poster
BudBrocken is offline Offline
9 posts
since Oct 2006
Oct 25th, 2006
0

Re: Please help a newbie - Javascript basic

Thanks everyone, I will give it a try.

Many many thanks.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
flub is offline Offline
3 posts
since Oct 2006

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: Javascript printing
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Is it possible to print a frame by clicking a button in a pop-up? (JAVASCRIPT)





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


Follow us on Twitter


© 2011 DaniWeb® LLC