Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

img loop 1

Status
Not open for further replies.

Tess21

ISP
May 11, 2006
2
GB
why doesn't this go to gif 2 then 3, 4, and 5 ?


<html>
<body>
<script type="text/javascript">
function curl(a){
var i = 1
if (a.substring(0,7)!=' || a.substring(a.length-3,a.length)!='gif')
{
alert("Error");
}
else
{
document.getElementById('dply').innerHTML='';
for (i = 1; i < 6; i++){
a=a.replace("*n*",i);
document.getElementById('dply').innerHTML = document.getElementById('dply').innerHTML +
'<br>'+'<img src="'+a+'"> <a href="'+a+'">'+a+'</a>';
}
}
}
</script>
<input type="text" id="url" size="40" value="<input type="button" onclick="curl(document.getElementById('url').value)">
<div id="dply">
</div>
</body>
</html>


thank you
 
<html>
<body>
<script type="text/javascript">
function curl(a){
var i = 1
if (a.substring(0,7)!=' || a.substring(a.length-3,a.length)!='gif')
{
alert("Error");
}
else
{
document.getElementById('dply').innerHTML='';
for (i = 1; i < 6; i++)
{
b=a;
b=b.replace("*n*",i);
document.getElementById('dply').innerHTML = document.getElementById('dply').innerHTML +
'<br>'+'<img src="'+b+'"> <a href="'+b+'">'+b+'</a>';
}
}
}
</script>
<input type="text" id="url" size="40" value="<input type="button" onclick="curl(document.getElementById('url').value)">
<div id="dply">
</div>
</body>
</html>


vlad
 
thank you, i'm new to this, can you tell me why i needed to add b=a; ?

thanks again.
 
You call the function with the value so the parameter a takes this value.

After that, in the first iteration when i has the value 1 the statement a=a.replace("*n*",i); changes the value of a, that will now be
In the next iterations, when i has the value 2, 3, 4... the statement a=a.replace("*n*",i); will do nothing, because the string *n* is not any more part of a, so the value of a will remain unchanged ->
If you insert b=a; and afterwards modify b, a will always keep the initial value and the string *n* will be found and replaced with 2, 3, 4...

Hope I was clear enough.


vlad
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top