[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.javascript

Problem determining iFrame source url ...

sean mc

1/29/2015 1:21:00 PM

I found code (someone elses code posted to another blog somewhere or other) that would change an iframes src attribute after a timed period. When used as below, it works fine....sort of... :

--------------------------------------------------------
<script type="text/javascript">
function ChangeSrc() {
var frames = Array('splash.html', 4,
'data.html');
var i = 0, len = frames.length;
document.getElementById('data_frame').src = frames[i++];
if (i >= len) return; // no more changing
setTimeout('ChangeSrc()', (frames[i++]*1000));
//}

window.onload = ChangeSrc;
</script>
--------------------------------------------------------

My problem with this is that is the user clicks on one of my available links to reload the iframe with another page before my second page has been loaded by the above script, they are then navigated away from the page the linked to.

So, I fished around the internet some more, and pieced together the following code to solve this :

--------------------------------------------------------
<script type="text/javascript">
function ChangeSrc() {
var frames = Array('jthl_splash.html', 4,
'jthl_hdata.html');
var i = 0, len = frames.length;
var iFrm = document.getElementById('data_frame');
var curUrl = GetIFrameURL(iFrm);
var chgUrl = CanChgSrc(curUrl);
if (chgUrl == true) {
iFrm.src = frames[i++];
}
else {
return;
}
if (i >= len) return; // no more changing
setTimeout('ChangeSrc()', (frames[i++]*1000));
}
function CanChgSrc(str) {
var opg = "about:blank";
var tpg = "jthl_splash.html";
var tUrl = str.substring(str.length - (tpg.length));
var oUrl = str.substring(str.length - (opg.length));
if (oUrl == opg || tUrl == tpg) {
return true;
}
else {
return false;
}
}
function GetIFrameURL (iFrm) {
if (isChromeBrowser() == true) {
var fUrl = iFrm.contentDocument.location.href;
}
else {
var fUrl = iFrm.contentWindow.location.href;
}
return fUrl;
}
function isChromeBrowser() {
var browsr = navigator.userAgent;
var patt = /Chrome/g;
return patt.test(browsr);
}
window.onload = ChangeSrc;
</script>
--------------------------------------------------------

This is to only let the page-reload-script run if the current iframe url is either "about:blank" or "splash.html", otherwise we wont switch to the "data.html" page.
This works fine the first time around.
GetIFrameURL is where the problem seems to reside.
The first time I hit it is fine...it returns "about.blank", and my "splash.html" page loads as expected.
The second time I hit it, code execution just stops after I hit it, and I do not reload..."splash.html" just remains in my iframe.

Can anyone see what my problem here is?
Is there a way to make this work?

Thanks in advance for any advice given.
Sean







15 Answers

Tim Streater

1/29/2015 1:39:00 PM

0

In article <c47f984c-b560-4c77-ac64-09a08a43ada7@googlegroups.com>,
sean mc <seanmc1217@gmail.com> wrote:

>I found code (someone elses code posted to another blog somewhere or other)
>that would change an iframes src attribute after a timed period. When used as
>below, it works fine....sort of... :
>
>--------------------------------------------------------
><script type="text/javascript">
>function ChangeSrc() {
> var frames = Array('splash.html', 4,
> 'data.html');
> var i = 0, len = frames.length;
> document.getElementById('data_frame').src = frames[i++];
> if (i >= len) return; // no more changing
> setTimeout('ChangeSrc()', (frames[i++]*1000));
>//}
>
>window.onload = ChangeSrc;
></script>
>--------------------------------------------------------

In this function, i is declared (var i = 0), incremented (by the i++),
and then compared with len.

I can't see why i would ever have a value other than 1 when this
statement:

if (i >= len) return; // no more changing

is executed. The same is true of your function. So what is the point of
the variable i or indeed the test to compare with len ??

--
"That which can be asserted without evidence, can be dismissed without
evidence."
-- Christopher Hitchens

sean mc

1/29/2015 1:59:00 PM

0

On Thursday, January 29, 2015 at 8:39:20 AM UTC-5, Tim Streater wrote:
> In article <c47f984c-b560-4c77-ac64-09a08a43ada7@googlegroups.com>,
> sean mc
>
> >I found code (someone elses code posted to another blog somewhere or other)
> >that would change an iframes src attribute after a timed period. When used as
> >below, it works fine....sort of... :
> >
> >--------------------------------------------------------
> ><script type="text/javascript">
> >function ChangeSrc() {
> > var frames = Array('splash.html', 4,
> > 'data.html');
> > var i = 0, len = frames.length;
> > document.getElementById('data_frame').src = frames[i++];
> > if (i >= len) return; // no more changing
> > setTimeout('ChangeSrc()', (frames[i++]*1000));
> >//}
> >
> >window.onload = ChangeSrc;
> ></script>
> >--------------------------------------------------------
>
> In this function, i is declared (var i = 0), incremented (by the i++),
> and then compared with len.
>
> I can't see why i would ever have a value other than 1 when this
> statement:
>
> if (i >= len) return; // no more changing
>
> is executed. The same is true of your function. So what is the point of
> the variable i or indeed the test to compare with len ??
>
> --
> "That which can be asserted without evidence, can be dismissed without
> evidence."
> -- Christopher Hitchens


This was originally designed to reload a more than just the two pages I am using it for at this time. This function can be used to load as many pages as the user would like to add to the array "frames"

i=0;
.....src = frames[i++]; sets the src attribute to frames[0], then increments i by 1.
i=1 now
frames[1] = 4. This is used to set the time out.
setTimeout('ChangeSrc()', (frames[i++]*1000);
after the timout is set, i now equals 2
This is now greater than the length of the array, so we exit the function
if (i >= len) return;

This part works fine.

The problem appears to be in

>------------------------------------------
>function GetIFrameURL (iFrm) {
> if (isChromeBrowser() == true) {
> var fUrl = iFrm.contentDocument.location.href;
> }
> else {
> var fUrl = iFrm.contentWindow.location.href;
> }
> return fUrl;
>}
>------------------------------------------

this is where code execution just stops.





Christoph M. Becker

1/29/2015 2:05:00 PM

0

Tim Streater wrote:

> In article <c47f984c-b560-4c77-ac64-09a08a43ada7@googlegroups.com>,
> sean mc <seanmc1217@gmail.com> wrote:
>
>> below, it works fine....sort of... :
>> --------------------------------------------------------
>> <script type="text/javascript">
>> function ChangeSrc() {
>> var frames = Array('splash.html', 4,
>> 'data.html');
>> var i = 0, len = frames.length;
>> document.getElementById('data_frame').src = frames[i++];
>> if (i >= len) return; // no more changing
>> setTimeout('ChangeSrc()', (frames[i++]*1000));
>> //}
>>
>> window.onload = ChangeSrc;
>> </script>
>> --------------------------------------------------------
>
> In this function, i is declared (var i = 0), incremented (by the i++),
> and then compared with len.
>
> I can't see why i would ever have a value other than 1 when this
> statement:
>
> if (i >= len) return; // no more changing
>
> is executed. The same is true of your function. So what is the point of
> the variable i or indeed the test to compare with len ??

I guess that i is supposed to be declared outside of ChangeSrc().
However, as the code has a syntax error anyway (the function body is not
terminated with }), all bets are off -- and certainly, it does not work
fine.

--
Christoph M. Becker

sean mc

1/29/2015 2:25:00 PM

0

On Thursday, January 29, 2015 at 9:04:43 AM UTC-5, Christoph M. Becker wrote:
> Tim Streater wrote:
>
> > In article <c47f984c-b560-4c77-ac64-09a08a43ada7@googlegroups.com>,
> > sean mc wrote:
> >
> >> below, it works fine....sort of... :
> >> --------------------------------------------------------
> >> <script type="text/javascript">
> >> function ChangeSrc() {
> >> var frames = Array('splash.html', 4,
> >> 'data.html');
> >> var i = 0, len = frames.length;
> >> document.getElementById('data_frame').src = frames[i++];
> >> if (i >= len) return; // no more changing
> >> setTimeout('ChangeSrc()', (frames[i++]*1000));
> >> //}
> >>
> >> window.onload = ChangeSrc;
> >> </script>
> >> --------------------------------------------------------
> >
> > In this function, i is declared (var i = 0), incremented (by the i++),
> > and then compared with len.
> >
> > I can't see why i would ever have a value other than 1 when this
> > statement:
> >
> > if (i >= len) return; // no more changing
> >
> > is executed. The same is true of your function. So what is the point of
> > the variable i or indeed the test to compare with len ??
>
> I guess that i is supposed to be declared outside of ChangeSrc().
> However, as the code has a syntax error anyway (the function body is not
> terminated with }), all bets are off -- and certainly, it does not work
> fine.
>
> --
> Christoph M. Becker

OK...I apologise..I see what was being pointed out.
I wasnt trying to sound snarky with my last reply...I just didnt see what was being pointed out, obviously. Function "ChangeSrc" is terminated correctly in my code I believe...am I missing it? Somehow it got commented out in the re-post...but I believe that closing "}" is present in my initial post without being commented out. I believe this was another Copy/Paste error. My apologies.

Sorry...that declaration of i=0 is supposed to be outside. That mistake was also somehow made by me when I was copying and pasting my code into the group editor.
My error....again...my apologies.

Now...back to my original statement ...my pages will increment correctly, and load into my ifrmame as expected...until I added the function :

function GetIFrameURL (iFrm) {
if (isChromeBrowser() == true) {
var fUrl = iFrm.contentDocument.location.href;
}
else {
var fUrl = iFrm.contentWindow.location.href;
}
return fUrl;
}

It goes through this the first time
var fUrl = iFrm.contentDocument.location.href; returns "about:blank" the first time thru.

The second time thru, it hit this line, and execution just stops.

sean mc

1/29/2015 2:32:00 PM

0

This is my code copied and pasted directly from my web page (hopefully without and dumb errors :-):

<script type="text/javascript">

var i = 0, len = frames.length;

function ChangeSrc() {
var frames = Array('splash.html', 4,
'data.html');
var iFrm = document.getElementById('data_frame');
var curUrl = GetIFrameURL(iFrm);
var chgUrl = CanChgSrc(curUrl);
if (chgUrl == true) {
iFrm.src = frames[i++];
}
else {
return;
}
if (i >= len) return; // no more changing
setTimeout('ChangeSrc()', (frames[i++]*1000));
}
function CanChgSrc(str) {
var opg = "about:blank";
var tpg = "splash.html";
var tUrl = str.substring(str.length - (tpg.length));
var oUrl = str.substring(str.length - (opg.length));
if (oUrl == opg || tUrl == tpg) {
return true;
}
else {
return false;
}
}
function GetIFrameURL (iFrm) {
if (isChromeBrowser() == true) {
var fUrl = iFrm.contentDocument.location.href;
}
else {
var fUrl = iFrm.contentWindow.location.href;
}
return fUrl;
}
function isChromeBrowser() {
var browsr = navigator.userAgent;
var patt = /Chrome/g;
return patt.test(browsr);
}

//function ChangeSrcSafe() {
// var frames = Array('splash.html', 4,
// 'data.html');
// var i = 0, len = frames.length;
// document.getElementById('data_frame').src = frames[i++];
// alert (document.getElementById('data_frame').src);
// if (i >= len) return; // no more changing
// setTimeout('ChangeSrc()', (frames[i++]*1000));
//}

window.onload = ChangeSrc;
</script>

sean mc

1/29/2015 4:10:00 PM

0

Sorry...Ive screwed this post up so much with pasting mistakes...
This is the code that has the problem I described.

<script type="text/javascript">

var frames = Array('jthl_splash.html', 4,
'jthl_hdata.html');
var i = 0, len = frames.length;

function ChangeSrc() {
var iFrm = document.getElementById('data_frame');
var curUrl = GetIFrameURL(iFrm);
var chgUrl = CanChgSrc(curUrl);

if (chgUrl == true) {
iFrm.src = frames[i++];
}
else {
return;
}
if (i >= len) return; // no more changing
setTimeout('ChangeSrc()', (frames[i++]*1000));
}
function CanChgSrc(str) {
var opg = "about:blank";
var tpg = "jthl_splash.html";
var tUrl = str.substring(str.length - (tpg.length));
var oUrl = str.substring(str.length - (opg.length));
if (oUrl == opg || tUrl == tpg) {
return true;
}
else {
return false;
}
}
function GetIFrameURL (iFrm) {
if (isChromeBrowser() == true) {
var fUrl = iFrm.contentDocument.location.href;
}
else {
var fUrl = iFrm.contentWindow.location.href;
}
return fUrl;
}
function isChromeBrowser() {
var browsr = navigator.userAgent;
var patt = /Chrome/g;
return patt.test(browsr);
}

//function ChangeSrcSafe() {
// var frames = Array('jthl_splash.html', 4,
// 'jthl_hdata.html');
// var i = 0, len = frames.length;
// document.getElementById('data_frame').src = frames[i++];
// alert (document.getElementById('data_frame').src);
// if (i >= len) return; // no more changing
// setTimeout('ChangeSrc()', (frames[i++]*1000));
//}

window.onload = ChangeSrc;
</script>

sean mc

1/29/2015 4:23:00 PM

0

Sorry..I have screwed this post up terribly.
This is the code that worked, with the exception of determining the current URl of the iframe. Code execution halts inexplicably at that point. (function GetIFrameURL ... .href line)


<script type="text/javascript">

var frames = Array('splash.html', 4,
'data.html');
var i = 0, len = frames.length;

function ChangeSrc() {
var iFrm = document.getElementById('data_frame');
var curUrl = GetIFrameURL(iFrm);
var chgUrl = CanChgSrc(curUrl);

alert("after URL determined : " + curUrl);

if (chgUrl == true) {
iFrm.src = frames[i++];
}
else {
return;
}
if (i >= len) return; // no more changing
setTimeout('ChangeSrc()', (frames[i++]*1000));
}
function CanChgSrc(str) {
var opg = "about:blank";
var tpg = "splash.html";
var tUrl = str.substring(str.length - (tpg.length));
var oUrl = str.substring(str.length - (opg.length));
if (oUrl == opg || tUrl == tpg) {
return true;
}
else {
return false;
}
}
function GetIFrameURL (iFrm) {
if (isChromeBrowser() == true) {
var fUrl = iFrm.contentDocument.location.href;
}
else {
var fUrl = iFrm.contentWindow.location.href;
}
return fUrl;
}
function isChromeBrowser() {
var browsr = navigator.userAgent;
var patt = /Chrome/g;
return patt.test(browsr);
}

//function ChangeSrcSafe() {
// var frames = Array('splash.html', 4,
// 'data.html');
// var i = 0, len = frames.length;
// document.getElementById('data_frame').src = frames[i++];
// alert (document.getElementById('data_frame').src);
// if (i >= len) return; // no more changing
// setTimeout('ChangeSrc()', (frames[i++]*1000));
//}

window.onload = ChangeSrc;
</script>

sean mc

1/30/2015 12:15:00 AM

0

OK...I have improved my code somewhat, although Im sure there is still plenty of room for more improvement.

The following works fine in Firefox(35.01) and IE(11), but still does NOT work in CHROME(41.0.2272.3 m) :

my "splash.html" page loads in Chrome, but then execution bombs out when it hits the
"location.href" lines.

In IE and FF, both pages (splash.html then data.html) load as long as the iFrame content hasn't been changed manually. This is good.

Does anyone know why the following code doesnt work in Chrome? Please? This is driving me nuts! What will work in Chrome as well ?

Thanks in advance
------------------------------------------------------------
<script type="text/javascript">

var frames = Array('jthl_splash.html', 4,
'jthl_hdata.html');
var i = 0, len = frames.length;

function ChangeSrc() {
var iFrm = document.getElementById('data_frame');
var iDoc = (iFrm.contentWindow || iFrm.contentDocument);
var curLoc = iDoc.location;
var curURL = curLoc.href;
if (CanChgSrc(curURL) == true) {
iFrm.src = frames[i++];
}
else {
return;
}
if (i >= len) return; // no more changing
setTimeout('ChangeSrc()', (frames[i++]*1000));
}
function CanChgSrc(myURL) {
if (myURL == "about:blank" || myURL.indexOf("jthl_splash.html") != -1) {
return true;
}
else {
return false;
}
}
window.onload = ChangeSrc;
</script>
------------------------------------------------------------

Evertjan.

1/30/2015 9:25:00 AM

0

sean mc <seanmc1217@gmail.com> wrote on 30 jan 2015 in
comp.lang.javascript:

> Thanks in advance
> ------------------------------------------------------------
> <script type="text/javascript">
>
> var frames = Array('jthl_splash.html', 4,
> 'jthl_hdata.html');
> var i = 0, len = frames.length;
>
> function ChangeSrc() {
> var iFrm = document.getElementById('data_frame');

var iDoc = (iFrm.contentWindow || iFrm.contentDocument);
console.log('#### i = ' + i);
console.log('#### iDoc = ' + iDoc);
var curLoc = iDoc.location;
console.log('#### curLoc = ' + curLoc);
var curURL = curLoc.href;
console.log('#### curURL = ' + curURL);

etc.
> ------------------------------------------------------------

This will give you ***in Chrome*** the following console reading:

============================
test.html:11 #### i = 0
test.html:12 #### iDoc = [object Window]
test.html:14 #### curLoc = about:blank
test.html:16 #### curURL = about:blank

[4 seconds later followed by the second loop:]

test.html:11 #### i = 2
test.html:12 Uncaught SecurityError: Blocked a frame with origin "null" from
accessing a frame with origin "null". Protocols, domains, and ports must
match.test.html:12 ChangeSrcVM8574:1 (anonymous function)
============================

So in the second loop

var iDoc = (iFrm.contentWindow || iFrm.contentDocument);

is not allowed.

So start changing that,
I am not at all shure what you are aiming at with this rather silly code.

=========================================================

btw, why not simplify code like:

> if (CanChgSrc(curURL) == true) {
> iFrm.src = frames[i++];
> }
> else {
> return;
> }

into:

if (!CanChgSrc(curURL)) return;
iFrm.src = frames[i++];

======================================

and:

> function CanChgSrc(myURL) {
> if (myURL=="about:blank" || myURL.indexOf("test3.html")!= -1){
> return true;
> }
> else {
> return false;
> }
> }

into:

function CanChgSrc(myURL) {
return myURL=="about:blank" || myURL.indexOf("test3.html")!=-1;
};

=================

or skip the whole CanChgSrc() function by changing my line:

> if (!CanChgSrc(curURL)) return;

into:

if (!(curURL=="about:blank"||curURL.indexOf("test3.html")!=-1)) return;

which can be symplified into:

if (curURL!="about:blank" && curURL.indexOf("test3.html")==-1) return;

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

sean mc

1/30/2015 2:54:00 PM

0

On Friday, January 30, 2015 at 4:25:48 AM UTC-5, Evertjan. wrote:
> sean mc wrote on 30 jan 2015 in
> comp.lang.javascript:
>
> > Thanks in advance
> > ------------------------------------------------------------
> > <script type="text/javascript">
> >
> > var frames = Array('jthl_splash.html', 4,
> > 'jthl_hdata.html');
> > var i = 0, len = frames.length;
> >
> > function ChangeSrc() {
> > var iFrm = document.getElementById('data_frame');
>
> var iDoc = (iFrm.contentWindow || iFrm.contentDocument);
> console.log('#### i = ' + i);
> console.log('#### iDoc = ' + iDoc);
> var curLoc = iDoc.location;
> console.log('#### curLoc = ' + curLoc);
> var curURL = curLoc.href;
> console.log('#### curURL = ' + curURL);
>
> etc.
> > ------------------------------------------------------------
>
> This will give you ***in Chrome*** the following console reading:
>
> ============================
> test.html:11 #### i = 0
> test.html:12 #### iDoc = [object Window]
> test.html:14 #### curLoc = about:blank
> test.html:16 #### curURL = about:blank
>
> [4 seconds later followed by the second loop:]
>
> test.html:11 #### i = 2
> test.html:12 Uncaught SecurityError: Blocked a frame with origin "null" from
> accessing a frame with origin "null". Protocols, domains, and ports must
> match.test.html:12 ChangeSrcVM8574:1 (anonymous function)
> ============================
>
> So in the second loop
>
> var iDoc = (iFrm.contentWindow || iFrm.contentDocument);
>
> is not allowed.
>
> So start changing that,
> I am not at all shure what you are aiming at with this rather silly code.
>
> =========================================================
>
> btw, why not simplify code like:
>
> > if (CanChgSrc(curURL) == true) {
> > iFrm.src = frames[i++];
> > }
> > else {
> > return;
> > }
>
> into:
>
> if (!CanChgSrc(curURL)) return;
> iFrm.src = frames[i++];
>
> ======================================
>
> and:
>
> > function CanChgSrc(myURL) {
> > if (myURL=="about:blank" || myURL.indexOf("test3.html")!= -1){
> > return true;
> > }
> > else {
> > return false;
> > }
> > }
>
> into:
>
> function CanChgSrc(myURL) {
> return myURL=="about:blank" || myURL.indexOf("test3.html")!=-1;
> };
>
> =================
>
> or skip the whole CanChgSrc() function by changing my line:
>
> > if (!CanChgSrc(curURL)) return;
>
> into:
>
> if (!(curURL=="about:blank"||curURL.indexOf("test3.html")!=-1)) return;
>
> which can be symplified into:
>
> if (curURL!="about:blank" && curURL.indexOf("test3.html")==-1) return;
>
> --
> Evertjan.
> The Netherlands.
> (Please change the x'es to dots in my emailaddress)


Thank you very much for the helpful reply, Evertjan.
I will work with what you have shown me and see what I can come up with.