Chrome 44 no longer supports the following syntax: window.location.href = "javascript:someFunction()"

时间:2015-07-28 15:44:22

标签: javascript html google-chrome

With the update of Chrome from 43 to 44, the following syntax no longer works:

window.location.href = "javascript:alert()"

I'm trying to load the contents of a page from local storage. I'm doing this by returning the page contents as the result of a javascript function call. I need to specify a URL as the target for window. Rather than specifying http://...., I used to be able to specify javascript as the scheme in the URL and specify the name of the function to invoke.

Apparently, Google took this feature away in version 44. Has anyone run into this and figured out an alternative?

2 个答案:

答案 0 :(得分:1)

对于其他人来说,这是一个官方的bug now

答案 1 :(得分:0)

语法适用于页面本身,但不适用于iframe。这曾经用于 Chrome 44 之前的版本。即使在 Chrome 44 中,也会构建文档并触发所有事件,但该页面在iframe中不可见。如果iframe的样式属性位置被删除并再次添加,则框架将开始显示内容。以下是说明 Chrome 44 的解决方法的示例代码。

<强> chrome44.html

<html>
    <script>
        function getFrame(theFrameID)
        {
            return document.getElementById(theFrameID);
        }
        function loadFrameSource()
        {
            var aFrame = getFrame("frm");
            aFrame.src = "chrome44frame.html";
            // had to add the following line as a workaround for Chrome 44
            aFrame.style.position = "absolute";
        }
    </script>
    <body onload="loadFrameSource()">
        <iframe id="frm" style="position : absolute;">
    </body>
</html>

<强> chrome44frame.html

<html>
    <script>
        function getHtml()
        {
            // Hard coding frame content for illustrating here, but actual script does more
            return "<html> <body onload=\"document.getElementById('evnt').innerText='onload event fired.';\">href  is Successful in changing html. <div id='evnt'>, but onload event didn't fire.</div></body> </html>";
        }
        function  loadPage()
        {
            document.location.href="javascript:getHtml('href')";
            // Had to add the following line as a workaround for Chrome 44
            window.parent.getFrame("frm").style.position = "";
        }
        loadPage();
    </script>
</html>