简体   繁体   中英

JavaScript isn't loaded when using link element

Im trying to make a website with JavaScript popups. Basically, when you click a link an alert or prompt comes up saying "You are now entering the [enter webpage name here] section."

For some reason, when I click the link, it will open the page but ignore any JavaScript! Here is the basic code:

HTML:

<html>
<head><link rel="text/javascript" href="effects.js" /></head>
<body>
<a href="common_code.html" onclick = "common_lang();">Click here</a>
</body>
</html>

JavaScript (effects.js):

function common_lang() {
    var enterCommon = prompt("You are now entering the Common Languages section.");
}

I don't get why this won't work! Does anyone have any ideas? Also, is there a way to make this more efficient? And I need the file in the same window so I can't use any window.open jazz. But efficiency isn't a priority, remember!

您应该使用script标签而不是link

<script type="text/javascript" src="effects.js">

First you need to change the link tag to script like:

<script type="text/javascript" src="effects.js"></script>

Second, this is because you are using the href attribute so the link just redirects you to common_code.html

if you want to navigate to the link based on user prompt selection you can do it like:

<a href="javascript:void(0)" onclick="common_lang();">Click here</a>

function common_lang() {
    var enterCommon = prompt("You are now entering the            Common Languages section.");
    if(enterCommon) {
        var a = document.createElement('a');
        a.href = "common_code.html";
        a.dispatchEvent(new Event('click'));
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM