Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #26757

Re: Problem determining iFrame source url ...

Newsgroups comp.lang.javascript
Date 2015-01-30 12:45 -0800
References <c47f984c-b560-4c77-ac64-09a08a43ada7@googlegroups.com>
Message-ID <df2ae8b0-c46e-4e6f-a4c4-de35c757bdbe@googlegroups.com> (permalink)
Subject Re: Problem determining iFrame source url ...
From sean mc <seanmc1217@gmail.com>

Show all headers | View raw


On Thursday, January 29, 2015 at 8:20:56 AM UTC-5, sean mc 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>
> --------------------------------------------------------
> 
> 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


OK...again, Id like to thank Evertjan for his help on this.

First, using his sugestions, I trimmed my "Silly Code" down to the following :

<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.location.href || iFrm.contentDocument.location.href);

console.log('#### i = ' + i);
console.log('#### iDoc = ' + iDoc);

  if (iDoc!="about:blank" && iDoc.indexOf("jthl_splash.html")==-1) return;
  iFrm.src = frames[i++];
  if (i >= len) return;                             // no more changing
  setTimeout('ChangeSrc()', (frames[i++]*1000));
}

window.onload = ChangeSrc;
</script>

Next, using console logging, which I wasnt aware of before Ertjan's post, I was able to see the error of :

>>> Blocked a frame with origin "null" from accessing a frame with origin "null". Protocols, domains, and ports must match <<<

After searching this error on the internet, I saw that this problem was caused simply by attempting to run my pages locally. Publishing them and running them solved my whole problem. Evidently this is a peculiarity of Chromes built in security policy.

Thank you to all who replied.

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Problem determining iFrame source url ... sean mc <seanmc1217@gmail.com> - 2015-01-29 05:20 -0800
  Re: Problem determining iFrame source url ... Tim Streater <timstreater@greenbee.net> - 2015-01-29 13:39 +0000
    Re: Problem determining iFrame source url ... sean mc <seanmc1217@gmail.com> - 2015-01-29 05:58 -0800
    Re: Problem determining iFrame source url ... "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-01-29 15:04 +0100
      Re: Problem determining iFrame source url ... sean mc <seanmc1217@gmail.com> - 2015-01-29 06:25 -0800
        Re: Problem determining iFrame source url ... sean mc <seanmc1217@gmail.com> - 2015-01-29 06:31 -0800
        Re: Problem determining iFrame source url ... sean mc <seanmc1217@gmail.com> - 2015-01-29 08:09 -0800
  Re: Problem determining iFrame source url ... sean mc <seanmc1217@gmail.com> - 2015-01-29 08:22 -0800
  Re: Problem determining iFrame source url ... sean mc <seanmc1217@gmail.com> - 2015-01-29 16:14 -0800
    Re: Problem determining iFrame source url ... "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-01-30 10:25 +0100
      Re: Problem determining iFrame source url ... sean mc <seanmc1217@gmail.com> - 2015-01-30 06:53 -0800
  Re: Problem determining iFrame source url ... sean mc <seanmc1217@gmail.com> - 2015-01-30 12:45 -0800
    Re: Problem determining iFrame source url ... "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-01-30 23:25 +0100
      Re: Problem determining iFrame source url ... "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-01-30 23:57 +0100
        Re: Problem determining iFrame source url ... sean mc <seanmc1217@gmail.com> - 2015-01-30 17:53 -0800
      Re: Problem determining iFrame source url ... sean mc <seanmc1217@gmail.com> - 2015-01-30 17:59 -0800

csiph-web