0

Hmm, so I have code that loads an iframe on a page called page2.html. The iframe currently loads the homepage of the site (http://mydomain.com). However, what I would like to do is place any page requested in an iframe as well.

So, like: http://mydomain.com/mypage/mysubpage

This needs to be grabbed from javascript/jquery and handed down into the iframe src value. This sounds easy enough, however, their is a global.js file that is redirecting to another page (page2.html) which than outputs the iframe (loading http://mydomain.com), so I'll need to pass a variable via javascript from the global.js function and put it into the page2.html page somehow to tell it to load the iframe for a different url instead of http://mydomain.com.

What's the best way to do this exactly? So, if you go here: http://cdficonnect.org in your desktop pc, you will notice that it loads the page in the iframe, but if you go here: http://www.cdficonnect.org/#/pages/articles/317 it still loads up the main page into the iframe and not that url. How to tell it to load up that url and not the main page?

Sounds simple enough, but having problems with implementing it.

Cookie Approach here:

In the redirector script, before it redirects...

function setCookie(c_name,value,exdays)
{
  var exdate=new Date();
  exdate.setDate(exdate.getDate() + exdays);
  var c_value=escape(value) + 
    ((exdays==null) ? "" : ("; expires="+exdate.toUTCString()));
  document.cookie=c_name + "=" + c_value + ";domain=cdficonnect.org";
}

setCookie('curURL', document.URL, 0);

In desktop.html, within the head:

function getCookie(c_name)
{
 var i,x,y,ARRcookies=document.cookie.split(";");
 for (i=0;i<ARRcookies.length;i++)
 {
  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
  x=x.replace(/^\s+|\s+$/g,"");
  if (x==c_name)
  {
   return unescape(y);
  }
 }
}

var curURL = getCookie('curURL');
$('iframe').attr('src', curURL);

This always displays the homepage within the iframe no matter what url I put into the address: http://cdficonnect.org

11
  • 1
    Either put the URL into a cookie, or add it as a query parameter in the redirect URL. Commented Nov 18, 2013 at 17:54
  • Have tried the cookie approach, but for some reason the URL isn't properly getting stored in the cookie... strange Commented Nov 18, 2013 at 18:39
  • Unless you show your code, we can't help you fix that. Commented Nov 18, 2013 at 19:05
  • Updated with code for how I'm setting/getting cookie. Commented Nov 18, 2013 at 19:10
  • 1
    The problem is that you're setting the cookie in cdiconnect.com, and trying to read it in www.cdiconnect.com. Since setCookie doesn't specify that the cookie should be available in subdomains, it can't read it. Try adding domain=cdiconnect.com; to the cookie. Commented Nov 18, 2013 at 19:28

1 Answer 1

1
+50

Try using HTML's localStorage :

In the redirector script, before it redirects...

function storeLocal(label,value)
{
  window.localStorage.setItem(label,value);
}

storeLocal('curURL', document.URL);

In desktop.html, within the head:

function getStorage(label)
{
 return window.localStorage.getItem(label);
}

var curURL = getStorage('curURL');
$('iframe').attr('src', curURL);

working jsFiddle : http://jsfiddle.net/RqkUk/

Sign up to request clarification or add additional context in comments.

4 Comments

I have to wait 19 more hours to award the bounty but this is exactly what I needed!!
@SolomonClosson Glad I could help ! Don't worry, take your time :)
@SolomonClosson I don't like doing this but if you could please remember about the bounty :D Thank you !
Thumbs UP, you got it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.