-3

script タグを動的に作成する以下のコードがあります。

<html>
<head>
    <title>Applying</title>
</head>
<body>

<script>
function getUrlVars() {
    var vars = {};
    var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, 
        function(m,key,value) {
            vars[key] = value;
        });
    return vars;
}
var variable1 = getUrlVars()["parameter1"];    

var myScript = document.createElement('script');

myScript.setAttribute('type', 'text/javascript');
myScript.setAttribute('urlId', '420');
myScript.setAttribute('dataTitle', variable1);
myScript.setAttribute('dataemail', 'admin@domain.net');

document.body.appendChild(myScript);                              
</script>

<input name="Apply" type="button" id="Apply" value="Apply" ONCLICK="window.location.href='https://www.google.com?'">

</body>
</html>

しかし、どういうわけか上記のコードは IE では機能しませんが、Chrome では問題なく機能します。よくわかりませんが、その理由は何ですか?誰でもそれを手伝ってもらえますか?

このすべてが IE では機能しません。

var myScript = document.createElement('script');

myScript.setAttribute('type', 'text/javascript');
myScript.setAttribute('urlId', '420');
myScript.setAttribute('dataTitle', variable1);
myScript.setAttribute('dataemail', 'admin@domain.net');

document.body.appendChild(myScript);    
4

1 に答える 1

1

次の関数を使用して、スクリプト要素を動的に追加できます。

   var create_script = function(data) {
        var script = document.createElement('script');
        script.type = 'text/javascript';
        script.text = data;

        //if you have to debug dynamically loaded script in chrome use the following - sourceURL has changed in the recent versions of chrome devetools.
        //script.text = data + "\n\n //@ sourceURL=" + ns + ".js\n";

        //append the script element to body or head or where it seems fit.
        document.body.appendChild(script);
    }

「データ」パラメータは、スクリプト タグの一部を形成する実際の JavaScript コード/URL です。コードにこれがありません。

編集: 非標準の属性については、http://www.w3schools.com/DTD/dtd_attributes.aspに従って doctype を変更することをお勧めします。

于 2013-07-28T17:44:18.197 に答える