Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-22-2012, 09:41 AM   PM User | #1
sallam
New Coder

 
Join Date: Oct 2004
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
sallam is an unknown quantity at this point
how to extract img src from an external list

Greetings

I need your help please..
I have a website of 20 pages. Each page displays an image from a catalog located in another domain, and the catalog changes each week, so I have to change the img src attribute each week too (the jpg images are named differently each week, though in the same directory). So instead of editing 20 html pages each time, I'm trying to find a way to control the img src from a text file, so that I edit only that text file once a week.

Is there a way to extract img src attributes from a text file or js file please?
For example I need page 1 to extract the first img src listed in the text file, and page 2 to extract the second src listed, etc.
I'm new to js so please give an example of the code that I should use.

Many thanks.
sallam is offline   Reply With Quote
Old 09-22-2012, 12:32 PM   PM User | #2
vwphillips
Senior Coder

 
Join Date: Mar 2005
Location: Portsmouth UK
Posts: 4,353
Thanks: 3
Thanked 456 Times in 443 Posts
vwphillips is a jewel in the roughvwphillips is a jewel in the roughvwphillips is a jewel in the rough
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
  <title></title>
</head>

<body>
<img id="tst" />

<script type="text/javascript">
/*<![CDATA[*/

function ajax(url,id,nu){
 var request=false;
 if (window.ActiveXObject){
  try {
   request=new ActiveXObject("Msxml2.XMLHTTP");
  }
  catch (e){
   try {
    request=new ActiveXObject("Microsoft.XMLHTTP");
   }
   catch (e){
   }
  }
 }
 else if (window.XMLHttpRequest){
  request=new XMLHttpRequest();
 }
 else {
  return false;
 }
 request.onreadystatechange=function(){ ajaxpopulate(request,document.getElementById(id),nu); }
 request.open('GET',url+'?'+new Date().getTime(),true);
 try {
  request.send(null);
 }
 catch (e){
 }
}

function ajaxpopulate(request,obj,nu){
 if (request.readyState==4&&(request.status==200||window.location.href.indexOf("http")==-1)){
  var src=request.responseText.split('\n')[nu];
  if (obj&&src){
   obj.src=src;
  }
 }
}

ajax('ajaximg.txt','tst',1)
/*]]>*/
</script>
</body>

</html>
ajaximg.txt

Code:
http://www.vicsjavascripts.org.uk/StdImages/Egypt5.jpg
http://www.vicsjavascripts.org.uk/StdImages/Egypt6.jpg
http://www.vicsjavascripts.org.uk/StdImages/Egypt7.jpg
__________________
Vic

God Loves You and will never love you less.

http://www.vicsjavascripts.org.uk/

If my post has been useful please donate to http://www.operationsmile.org.uk/
vwphillips is offline   Reply With Quote
Old 09-22-2012, 12:46 PM   PM User | #3
sallam
New Coder

 
Join Date: Oct 2004
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
sallam is an unknown quantity at this point
Thanks very much.
I've tried your code (created test.html and ajaximg.txt in my pc desktop), but the test page didn't display any images. Do the files need to be uploaded to a server for them to work?
sallam is offline   Reply With Quote
Old 09-22-2012, 03:27 PM   PM User | #4
vwphillips
Senior Coder

 
Join Date: Mar 2005
Location: Portsmouth UK
Posts: 4,353
Thanks: 3
Thanked 456 Times in 443 Posts
vwphillips is a jewel in the roughvwphillips is a jewel in the roughvwphillips is a jewel in the rough
both files need to be in the same directory

or the file path of ajaximg.txt needs to be specified in the call

in any event both files must be in the same domain
__________________
Vic

God Loves You and will never love you less.

http://www.vicsjavascripts.org.uk/

If my post has been useful please donate to http://www.operationsmile.org.uk/
vwphillips is offline   Reply With Quote
Old 09-23-2012, 06:53 AM   PM User | #5
sallam
New Coder

 
Join Date: Oct 2004
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
sallam is an unknown quantity at this point
Yes, both files are on my desktop. But the test.html page loads nothing. I'm confused.. what is wrong?..
I tried the images and each loads fine on their own browser tabs. But still the page loads no images...
sallam is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:16 PM.


Advertisement
Log in to turn off these ads.