简体   繁体   中英

Unable to call external javascript in aspx page

I am a newbie to jquery. Help me out to solve this thing

I have this script in a separate js file

$(document).ready(function () {
$("#btn_click").click(function () {
    $("#SecondLine").hide();
});
});

I want to execute this script in a click on the element with id="btn_click"

<form id="form1">
<div>
    <h2>This is a heading</h2>
    <p>This is a paragraph.</p>
    <p id="SecondLine">This is another paragraph.</p>       
    <p1 id="btn_click">Click me</p1>
</div>
</form>

The code works perfectly when i include the whole script between the head tag on the same aspx page. but it is not working when i have this code in a separate js file. Note: I have included the script in aspx page.

<script src="../Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>
<script src="../Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script src="../Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script src="../JQuery/JQuery_HidingText.js" type="text/javascript"></script>

Still not working :( .Did i Missed anything ?

You're load jQuery scripy twice.

<script src="../Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script src="../Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>

Remove one of them.

You should remove this file too:

<script src="../Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>

After that check paths to scripts. Your script work correctly .

Use ~ symbol instead ..

~/Scripts/jquery-1.4.1.js" instead of ../Scripts/jquery-1.4.1.js" ~/Scripts/jquery-1.4.1.min.js" instead of ../Scripts/jquery-1.4.1.min.js"

All the Best

Try using

pageLoad()

instead of

$(document).ready(function ()
{
  //.. etc
}); 

The pageLoad uses a different method (timeouts) to check whether the DOM is ready. I had a script that failed with $(document).ready but worked with pageLoad - pageLoad hitting a fraction later which made the difference. This was only an issue in IE for me

Anyway might not work but easy to try and worth a go i reckon

See here for details pageLoad

And of course good luck

Another thought

Change the location of the external script file reference to just before the closing body tag rather than in the head. Again to give the markup the best chance of loading before the script executes (i know the document ready should do this but ...). Again worth a go

And

Just for test purpose i would change the external script file to be as simple as possible just to rule any other wierdness out ie use

$(document).ready(function () {
    alert('Hello - i am here');
});

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