-1

I am currently building a website as part of my Media Coursework for Year 11 but I can't seem to get this If...Else... function to work. Here is my coding.

if(ifr.contentDocument.getElementById("ctl00_ctl00_cphBanner_MenuRedesign_BannerAlertsAndOptionsLoginView_BannerAlertsAndOptions_Authenticated_MessagesTextWrapper") != null){
    notify("Personalising site...")
    document.getElementById("loginButton").innerHTML = "Account Connected!";
    document.getElementById("loginButton").onclick=function(){window.location.href="//roblox.com/--place?id=91276386";}
    notify("Account connected successfully");
    hideWheel();
}else{
    notify("Could not connect account");
    hideWheel();
};

I removed the "if(...){...}else{...};" from the code and found it worked, so I'm sure that the fault is in the "if(...)" itself.

If it helps, ifr was actually created using document.createElement("iframe"); The iFrame does have a "src" and does show up on the page with the content displayed.

Any help is appreciated. Thanks.

TheZ
  • 3,663
  • 19
  • 34
celliott1997
  • 1,115
  • 1
  • 8
  • 17
  • is there an error shown in the console? – Janus Troelsen Sep 28 '12 at 21:54
  • 1
    If the iframe loading an external URL, you cannot access its content. – Felix Kling Sep 28 '12 at 21:55
  • Why are you putting `hideWheel()` in both conditional paths? Just put it outside the conditional if you want it to always run. – TheZ Sep 28 '12 at 21:56
  • first check your browser console for javascript error. Am not sure the getElementById would understand those jargons. Just joking :) – codingbiz Sep 28 '12 at 21:56
  • 2
    There is an error: SCRIPT5007: Unable to get value of the property 'getElementById': object is null or undefined rbxLaunchSite.htm, line 33 character 1 – celliott1997 Sep 28 '12 at 21:56
  • 1
    @3659905 there is your answer. ifr.contentDocument.document or similar. see http://stackoverflow.com/questions/2947082/whats-the-most-concise-cross-browser-way-to-access-an-iframe-elements-window – Magicianeer Sep 28 '12 at 22:01

2 Answers2

1

Try to use:

ifr.contentWindow.document

You can also refer to: How can I access iframe elements with Javascript?

Community
  • 1
  • 1
LRA
  • 1,057
  • 13
  • 18
0

Try this:

var x = document.getElementById("ctl00_ctl00_cphBanner_MenuRedesign_BannerAlertsAndOptionsLoginView_BannerAlertsAndOptions_Authenticated_MessagesTextWrapper");
var y = (x.contentWindow || x.contentDocument);

if(y.document){

    notify("Personalising site...")
    document.getElementById("loginButton").innerHTML = "Account Connected!";
    document.getElementById("loginButton").onclick=function(){window.location.href="//roblox.com/--place?id=91276386";}
    notify("Account connected successfully");
    hideWheel();

}
else{

    notify("Could not connect account");
    hideWheel();

};

Is the iframe that you try to access and page from which you try to access on the same domain? If it is on another domain you can't access it because of cross domain blockade!

Also I am suprised by that long ID, is that ID correct?

Develoger
  • 3,950
  • 2
  • 24
  • 38
  • No, the page that the iframe is trying to access is a games website. The ID is correct, they use very long IDs on most things (after inspecting their website). – celliott1997 Sep 28 '12 at 22:04
  • If it is on another domain I am sorry to tell that you can't access it because of cross domain blockade :( – Develoger Sep 28 '12 at 22:05