简体   繁体   English

如何将此javascript代码转换为jquery

[英]How to convert this javascript code in to jquery

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. 我已经尝试了很多东西,但没有成功为跨浏览器编写东西。

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: 具体来说,IE8或更早版本不支持.textContent ,因此:

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();
    }
}

Here's some jQuery conceptual material to get you started: 这里有一些jQuery概念材料可以帮助您入门:

// 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. 请记住以下重要事实:在完成父文档后加载框架,这意味着在加载框架之前将执行ready()函数。 If you try to access your frame in your ready() function, most likely you will get nothing because it's not been loaded yet -- ie, it's race condition. 如果你尝试在ready()函数中访问你的框架,很可能你什么也得不到,因为它尚未被加载 - 即它是竞争条件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM