动态加载外部脚本不起作用

时间:2016-10-31 17:09:59

标签: javascript

我正在建立一个单页的网站。将外部html附加到另一个html中。 CSS加载HTML但外部脚本不加载?

<button>
    <a class="cd-btn-services" href="technical-services.html" id="mylink" data-type="page-transition">click me</a>
</button>


var myLink = document.getElementById('mylink');

myLink.onclick = function(){
    var script = document.createElement("script");
    script.type = "text/javascript";
    script.src = "Public/Scripts/filename.js."; 
    document.getElementsByTagName("head")[0].appendChild(script);
    return false;
}

1 个答案:

答案 0 :(得分:0)

你的src中有错误:

script.src = "Public/Scripts/filename.js";

script.src = "Public/Scripts/filename.js.";

你的代码为我工作