9 Replies - 4844 Views - Last Post: 17 October 2011 - 09:55 AM

#1 p00ndawg  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 82
  • Joined: 09-November 09

How can i loop through different iframe sources?

Posted 31 August 2011 - 06:23 AM

i am displaying a pdf using an iframe. i am going to be storing links to a lot of pdfs in a database.
So i want to be able to loop through and change the iframe src based on a button click.

is there anyway I can do this?

For reference i am writing the code behind in vb .net.
The pdf links will be directory links more than likely, I just need to be able to store whether or not a user was able to see a certain pdf, but thats another question for another day :D

This post has been edited by p00ndawg: 31 August 2011 - 06:43 AM


Is This A Good Question/Topic? 0
  • +

Replies To: How can i loop through different iframe sources?

#2 VolcomMky  Icon User is offline

  • D.I.C Regular

Reputation: 74
  • View blog
  • Posts: 315
  • Joined: 13-May 09

Re: How can i loop through different iframe sources?

Posted 31 August 2011 - 07:59 AM

You can grab all frames in the page with DOM

<script>
var frames = document.getElementsByTagName('iframe'); 
for (var i = 0; i < frames.length; i++){ 
    fSrc = frames[i].src; 
}
</script>

Was This Post Helpful? 0
  • +
  • -

#3 p00ndawg  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 82
  • Joined: 09-November 09

Re: How can i loop through different iframe sources?

Posted 31 August 2011 - 08:12 AM

thank you for the reply but I am not quite sure this is what I am trying to do.
I have several pdf's stored in a directory/DB, so I want one pdf to show, then have another one come up when the user clicks that they have seen it.
I dont know how many pdf's there will be so I cant embed a lot of iframes. I need to inject a new src.

This post has been edited by p00ndawg: 31 August 2011 - 08:12 AM

Was This Post Helpful? 0
  • +
  • -

#4 VolcomMky  Icon User is offline

  • D.I.C Regular

Reputation: 74
  • View blog
  • Posts: 315
  • Joined: 13-May 09

Re: How can i loop through different iframe sources?

Posted 31 August 2011 - 08:44 AM

You can try creating an array of sources and loop through on click

<script>
var currentSource = 0;
var sources = new Array();
sources[0] = "pdf url";
sources[1] = "pdf url";
sources[3] = "pdf url";
sources[4] = "pdf url";
sources[5] = "pdf url";
sources[6] = "pdf url";

function loadSource()
{
    currentSource++;
    nextSource = currentSource;
    top.frames['YourFrame'].location.href = sources[nextSource];
}
</script>

<input type="button" value="Next PDF" onclick="loadSource()">

This post has been edited by VolcomMky: 31 August 2011 - 08:45 AM

Was This Post Helpful? 1
  • +
  • -

#5 p00ndawg  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 82
  • Joined: 09-November 09

Re: How can i loop through different iframe sources?

Posted 31 August 2011 - 09:05 AM

<script language="Javascript" type="text/Javascript">
var currentSource = 0;	var sources = new Array();
    sources[0] = "Z:pdflocation";
    sources[1] = "Z:pdflocation";
     
	function loadSource()
	{
	    currentSource++;
	    nextSource = currentSource;
	    top.frames['test'].location.href = sources[nextSource];
	}
</script>
<iframe id="test" width="50%" height="700px" scrolling="no" frameborder="0">
</iframe>
<input type="button" value="Next PDF" onclick="loadSource()" />



I am trying this, but it doesnt quite seem to work. I get a dead page in the iframe.
Was This Post Helpful? 0
  • +
  • -

#6 VolcomMky  Icon User is offline

  • D.I.C Regular

Reputation: 74
  • View blog
  • Posts: 315
  • Joined: 13-May 09

Re: How can i loop through different iframe sources?

Posted 31 August 2011 - 09:26 AM

Is this hosted locally or on the web?
Was This Post Helpful? 0
  • +
  • -

#7 p00ndawg  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 82
  • Joined: 09-November 09

Re: How can i loop through different iframe sources?

Posted 31 August 2011 - 10:33 AM

View PostVolcomMky, on 31 August 2011 - 10:26 AM, said:

Is this hosted locally or on the web?



The Z drive is on a network.
Was This Post Helpful? 0
  • +
  • -

#8 Bob Loblaw  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 09-October 11

Re: How can i loop through different iframe sources?

Posted 09 October 2011 - 05:21 PM

Thanks, this is exactly what I was looking for. Well, pretty much. The only question I have is, how do I get it to loop back to the first source if the current source is the last source? I'm mostly a cut-and-paster with Javascript at this point, but I tried changing your code to:

<script>
var currentSource = 0;
var sources = new Array();
sources[0] = "slideshows/slideshow/IP-HauntedMuseum.html";
sources[1] = "slideshows/slideshow/case2.html";

function loadSource()
{
    currentSource++;
	if (currentSource-1)
	nextSource = 0
	else {
    nextSource = currentSource;}
    top.frames['frame1'].location.href = sources[nextSource];
}
</script>



It kind of worked. It loops once, but then gets stuck on the first source thereafter. I'm sure it's not hard to accomplish, but it's just beyond me at this point.

Any help would be much appreciated.
Thanks.


View PostVolcomMky, on 31 August 2011 - 08:44 AM, said:

You can try creating an array of sources and loop through on click

<script>
var currentSource = 0;
var sources = new Array();
sources[0] = "pdf url";
sources[1] = "pdf url";
sources[3] = "pdf url";
sources[4] = "pdf url";
sources[5] = "pdf url";
sources[6] = "pdf url";

function loadSource()
{
    currentSource++;
    nextSource = currentSource;
    top.frames['YourFrame'].location.href = sources[nextSource];
}
</script>

<input type="button" value="Next PDF" onclick="loadSource()">

Was This Post Helpful? 0
  • +
  • -

#9 VolcomMky  Icon User is offline

  • D.I.C Regular

Reputation: 74
  • View blog
  • Posts: 315
  • Joined: 13-May 09

Re: How can i loop through different iframe sources?

Posted 15 October 2011 - 02:30 PM

@Bob

You will want to reset the currentSource or else it will continue to get higher and higher
so when it hits the last one

if (currentSource==2)
{
    nextSource = 0;
    currentSource = 0;
}
else
{
    nextSource = currentSource;
}


I think using
if(currentSource==2)
will have the same results as
if(currentSource == sources.length)
But using sources.length will work with more or less items in the sources array instead of having to update the "2" to your new array count

This post has been edited by VolcomMky: 15 October 2011 - 02:32 PM

Was This Post Helpful? 0
  • +
  • -

#10 Bob Loblaw  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 09-October 11

Re: How can i loop through different iframe sources?

Posted 17 October 2011 - 09:55 AM

Thanks for the response. I thought I might have come in to the thread too late, so I came up with a solution that worked for me:

  
if (currentSource < sources.length-1){currentSource++}
		else {currentSource = 0}
    nextSource = currentSource;



I switched it to your code though, since I don't really know what I'm doing. It works perfectly. Thanks again Volcom!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1