سؤال

لدي مشكلة استدعاء وظيفة جافا سكريبت في iframe من الصفحة الرئيسية.هنا هو بلدي اثنين من الصفحات:

mainPage.html

<html>
<head>
    <title>MainPage</title>
    <script type="text/javascript">
        function Reset() 
        {
            if (document.all.resultFrame)
                alert("resultFrame found");
            else
                alert("resultFrame NOT found");

            if (typeof (document.all.resultFrame.Reset) == "function")
                document.all.resultFrame.Reset();
            else
                alert("resultFrame.Reset NOT found");
        }
    </script>
</head>
<body>
    MainPage<br>
    <input type="button" onclick="Reset()" value="Reset"><br><br>
    <iframe height="100" id="resultFrame" src="resultFrame.html"></iframe>
</body>
</html>

resultFrame.html

<html>
<head>
    <title>ResultPage</title>
    <script type="text/javascript">
        function Reset() 
        {
            alert("reset (in resultframe)");
        }
    </script>
</head>
<body>
    ResultPage
</body>
</html>

(وأنا أعلم أن document.all لا ينصح ولكن هذه الصفحة يجب أن ينظر إليها فقط مع IE داخليا و لا أعتقد أن المشكلة)

عندما كنت اضغط على إعادة تعيين زر أحصل على "resultFrame وجدت" و "resultFrame.إعادة تعيين"لم يتم العثور على.يبدو أن يكون إشارة إلى الإطار ولكن لا يمكن استدعاء الدالة على الإطار ، لماذا ؟

هل كانت مفيدة؟

المحلول

استخدام:

document.getElementById("resultFrame").contentWindow.Reset();

للوصول إلى إعادة تعيين وظيفة في iframe

document.getElementById("resultFrame") سوف تحصل على iframe في التعليمات البرمجية الخاصة بك ، contentWindow سوف تحصل على نافذة الكائن في iframe.مرة واحدة لديك نافذة الطفل ، يمكنك الرجوع إلى جافا سكريبت في هذا السياق.

انظر أيضا هنا ولا سيما الجواب من bobince.

نصائح أخرى

بدلا من الحصول على إطار من الوثيقة ، حاول الحصول على إطار من وجوه نافذة.

في المثال أعلاه تغيير هذا:

if (typeof (document.all.resultFrame.Reset) == "function")
    document.all.resultFrame.Reset();
else
    alert("resultFrame.Reset NOT found");

إلى

if (typeof (window.frames[0].Reset) == "function")
    window.frames[0].Reset();
else
    alert("resultFrame.Reset NOT found");

المشكلة هي أن نطاق جافا سكريبت داخل iframe لم يتعرض خلال دوم عنصر iframe.فقط نافذة الكائنات تحتوي على جافا سكريبت النطاق المعلومات عن الإطارات.

لمزيد من المتانة:

function getIframeWindow(iframe_object) {
  var doc;

  if (iframe_object.contentWindow) {
    return iframe_object.contentWindow;
  }

  if (iframe_object.window) {
    return iframe_object.window;
  } 

  if (!doc && iframe_object.contentDocument) {
    doc = iframe_object.contentDocument;
  } 

  if (!doc && iframe_object.document) {
    doc = iframe_object.document;
  }

  if (doc && doc.defaultView) {
   return doc.defaultView;
  }

  if (doc && doc.parentWindow) {
    return doc.parentWindow;
  }

  return undefined;
}

و

...
var el = document.getElementById('targetFrame');

var frame_win = getIframeWindow(el);

if (frame_win) {
  frame_win.reset();
  ...
}
...

الاتصال

window.frames['resultFrame'].Reset();

objectframe.contentWindow.Reset() عليك الرجوع إلى أعلى مستوى عنصر في الإطار الأول.

الشرط الأول والأساسي الذي يجب أن يتحقق هو أن كلا من الوالد و iframe يجب أن تنتمي إلى نفس المصدر.بمجرد أن يتم الطفل يمكن أن تحتج الأم باستخدام نافذة.فتحت أسلوب الوالدين يمكن أن تفعل الشيء نفسه بالنسبة للطفل كما ذكر أعلاه

عند الوصول إلى resultFrame من خلال الوثيقة.كل ذلك فقط تسحب على أنها عنصر HTML وليس إطار النافذة.يمكنك الحصول على نفس المشكلة إذا كان لديك إطار إطلاق حدث باستخدام "هذا" إشارة الذاتي.

محل:

document.all.resultFrame.Reset();

مع:

window.frames.resultFrame.Reset();

أو:

document.all.resultFrame.contentWindow.Reset();

إذا كنت لا يمكن استخدامها مباشرة و إذا واجهت هذا الخطأ:منعت الإطار مع الأصل "http://www..com" من الوصول إلى الصليب الأصل الإطار.يمكنك استخدام postMessage() بدلا من استخدام وظيفة مباشرة.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top