0

我有一个已发布的迷人 html 文件,该文件已加载到另一个 html 的 iframe 中。我无法在两者之间进行通信,甚至无法使用 localStorage。谁能告诉我我错过了什么?

父 html

    var everythingLoaded = setInterval(function () {
            if (/loaded|complete/.test(document.readyState)) {
                clearInterval(everythingLoaded);
                init();
            }
        }, 10);

        function init() {

            ScormProcessInitialize();
            var studentID = ScormProcessGetValue("cmi.core.student_id");
            var student_name = ScormProcessGetValue    ("cmi.core.student_name");
            var nameArraya = student_name.split(" ");
            var nameArrayb = nameArraya[1].split(",");
            var studentNumber = nameArrayb[0];
            ScormProcessSetValue("cmi.core.lesson_status", "incomplete");
            localStorage.setItem("_studentNumber", studentNumber);
            alert("Student Number: " + studentNumber + "  Student Mame: " + student_name);
            setTimeout(function () {
                  document.getElementById("iFrame_a").innerHTML = "<iframe name='iframe_1' id='frame_1' src='//somepath.com/sandbox/somecourse/index.html' frameborder='0' width='1000px' height='605px'></iframe>";
            }, 250);
        }

        function sendComplete() {
            alert("Send from index start!");
            ScormProcessSetValue("cmi.core.lesson_status", "completed");
            alert("send status: Completed");
        }
        window.onbeforeunload = function (){

            cpInfoCurrentSlide = localStorage.getItem("_cpInfoCurrentSlide")
            alert(cpInfoCurrentSlide);
            if(cpInfoCurrentSlide >= 40)
                {
            alert("onbeforeunload called: " + cpInfoCurrentSlide )
            ScormProcessSetValue("cmi.core.lesson_status", "completed");
                }
        }

iframe 代码片段

localStorage.setItem("_cpInfoCurrentSlide", cpInfoCurrentSlide);
4

1 回答 1

0

我相信您的问题出在 onbeforeunload 上。我记得迷人的包在加载时会破坏父框架中与 onbeforeunload 相关的任何函数。

试试这个,覆盖你的 SCORM api setvalue 方法:

var oldLMSSetValue = window.API.LMSSetValue;
window.API.LMSSetValue = function(key, value){
  if(key === 'cmi.core.lesson_status' && value === 'completed'){
    //do your stuff here
    cpInfoCurrentSlide = localStorage.getItem("_cpInfoCurrentSlide")
    alert(cpInfoCurrentSlide);
  }
  //call the original scorm api function so that it runs as expected.
  oldLMSSetValue(key,value);
};

编辑:此代码将进入父窗口,而不是 iframe。

于 2017-05-10T20:12:15.913 回答