How do you load .js files with ASP.NET?
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<script runat="server">开发者_C百科
Sub Page_Load(ByVal e As System.EventArgs)
Page.ClientScript.RegisterClientScriptInclude("basicjs", "basic.js")
End Sub
</script>
<title>RegisterClientScriptBlock Example</title>
</head>
<body>
<form runat="server">
<input type="button" id="button1" value="clickme" onclick="click()"/>
</form>
</body>
</html>
I'm using ASP.net (just learning) with VB. I can't seem to get Javascript code to work with ASP VB code. My main problem seems to be that .js files are not loading. In the example, basic.js just contains a function click()
that calls an alert()
message. How do I get .js files to load with ASP code?
You don't need any server-side code to load a JavaScript file. Just put this in your <head>
:
<script src="basic.js"></script>
JavaScript is a client-side script language. It's not something your server should execute; the client should.
ASP is a server-side language that generates some output that's presented to the user. Therefore, it cannot be ASP's responsibility to execute a JavaScript.
Instead, you should just include the JavaScript in your HTML, thus telling the client to execute it.
<script type="text/javascript" src="basic.js"></script>
It should be like...
Page.ClientScript.RegisterClientScriptInclude("JScripts", ResolveUrl("~/JSFolderName/basic.js"));
First check if the include tag is being rendered to the page by viewing the page source from the browser. Once you've established the include tag exists check the path to the script file.
This article explains how to resolve the path correctly
Basically use this in your onload event:
Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Page.ClientScript.RegisterClientScriptInclude("basicjs", ResolveClientUrl("~/basic.js"))
End Sub
Is the basic.js is accessible from the page? I mean if the page and js file located under one folder?
You can get more idea from here:
http://blog.janjonas.net/2011-01-19/asp_net-add-script-tags-include-javascript-master-pages-head-tag
Thanks!
Dhananjay
You need to include as follow
<script type="text/javascript">
// your code goes here.
</script>
if javascript is in file then follow
<script type="text/javascript" src="javascript_file_path">
// your code goes here.
</script>
精彩评论