0
if (window.parent.frames['scripts1']) {
  if (window.parent.frames['scripts1'].document.documentElement) {
    var strSCRIPT = window.parent.frames['scripts1'].document.documentElement.textContent;
    if ((strSCRIPT.lastIndexOf('bbbbEND') - strSCRIPT.length) != -7) {
      window.parent.frames['scripts1'].document.location.href = 'test1.txt?refresh=' + Date();
    }
  } else {
    window.parent.frames['scripts1'].document.location.href = 'test1.txt?refresh=' + Date();
  }
}

I have tried lot of things but not success in writing something for cross browser.

7
  • 13
    if it works, why fix it? Commented Jun 26, 2012 at 16:07
  • 3
    What have you tried? Commented Jun 26, 2012 at 16:08
  • 1
    jQuery is a library built on Javascript. Your intention shouldn't be to change your javascript to jQuery, as jQuery is javascript. If you have no issues with this code, I see no valid reason of "converting" it to jQuery. Commented Jun 26, 2012 at 16:09
  • IIRC, crossing frames through JS isn't going to be supported on all browsers. This also seems like a very convoluted and probably slow way to refresh content... Commented Jun 26, 2012 at 16:12
  • Unless pedantry is intended, people need to consider before commenting that when a user asks about converting JavaScript to jQuery, they're almost always asking about converting DOM API to jQuery API. This usually isn't that hard to figure out. Commented Jun 26, 2012 at 17:09

2 Answers 2

2

First, refactor your code to eliminate repetition:

var s = window.parent.frames['scripts1'];
if (s) {
    var d = s.document;
    var e = d.documentElement;
    var t = e ? e.textContent : null;
    if (!t || t.length - t.lastIndexOf('bbbbEND') != 7) {
        d.location.href = 'test1.txt?refresh=' + Date();
    }
}

There's one identified compatibility issue but at least now we've got a chance of spotting it!

Specifically, .textContent isn't supported in IE8 or earlier, hence:

var s = window.parent.frames['scripts1'];
if (s) {
    var d = s.document;
    var e = d.documentElement;
    var t = e ? (e.textContent || e.innerText) : null;
    if (!t || t.length - t.lastIndexOf('bbbbEND') != 7) {
        d.location.href = 'test1.txt?refresh=' + Date();
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

.textContent is not compatible with all commonly used browsers, but at least you've provided a positive reworking of the code.
@amnotiam hmm, maybe there is some scope for jQuery after all, then! Ah, it's only IE8 and lower that doesn't support it.
Well, if this is the only compatibility patch needed, then I'd say jQuery would be massive overkill. I'd rather see a quickie || e.innerText instead. ...and yes, IE8 and lower would be the only issue. Unfortunately IE8 still has significant usage, though dwindling.
@amnotiam I reverted out your edited as I was in the middle of another one - I wanted to show the original code refactored, and then show the cross browser fix.
.textContent works only for firfox and .innerText work for IE then what about other browser like chrome,safari etc
|
0

Here's some jQuery conceptual material to get you started:

// make sure to give your frame an ID, and then it's easy to access
var frame_a = $( "#frame_a" );        

// jQuery's content() function seems to only work on iFrames, not on Frames
var frame_a_document = $( frame_a[0].contentDocument );        

var frame_a_document_body = frame_a_document.find( "body" );
var frame_a_text = $( frame_a_document_body ).text();        

alert( frame_a_text );

Keep in mind this important fact: Your frame is loaded after the parent document is finalized which means that the ready() function will be executed before the frame is loaded. If you try to access your frame in your ready() function, most likely you will get nothing because it's not been loaded yet -- i.e., it's race condition.

Comments

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.