有人能告诉我为什么这个脚本不能运行吗?

Can someone tell me why this script doesn't run?

本文关键字:不能 脚本 运行 告诉我 为什么      更新时间:2023-09-26

当我打开这个页面没有任何反应。那么这段代码的错误是什么呢?
这是代码:

<!doctype html>
    <html>
    <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <script type="text/javascript">
        function code(){
        var filename = loc.pathname.split("/");
        filename = filename[pathname.length-1];
        alert(filename);
    <iframe src="http://url/" +currentPageUrl scrolling="yes" style="position:fixed; top:0px; left:0px; bottom:0px; right:0px; width:100%; height:100%; border:none; margin:0; padding:0; overflow:hidden; z-index:999999;">
        Your browser doesn't support IFrames
        </iframe>`
        }
        </script>
<title>Index</title>
</head>
<body onload="code();">
</body>
</html>

我不知道你想做什么,但是你的代码中有很多错误。

loc应为location

我猜你在做类似的事情。

<!doctype html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <script type="text/javascript">
            function code() {
                var filename = location.pathname.substring(location.pathname.lastIndexOf("/") + 1);
                document.body.innerHTML = "<iframe src=" + filename + " scrolling='yes' style='position:fixed; top:0px; left:0px; bottom:0px; right:0px; width:100%; height:100%; border:none; margin:0; padding:0; overflow:hidden; z-index:999999;'>your browser doesn't support iframe<iframe>";
            }
        </script>
        <title>Index</title>
    </head>
    <body onload="code();">
    </body>
</html>

因为你没有一个有效的脚本。

  1. loc未定义
  2. 你只是有一个iFrame标签飞来飞去的JS代码。

这段代码有几个问题:

  1. loc看起来没有定义。也许你想要location

  2. 要向DOM添加代码,可以使用:

    document.body.innerHTML = "<iframe src='http://' + variable ></iframe>"

  3. 确保所有其他变量都已定义。例如,在获取长度时使用location.pathname