本文介绍了如何从外部文件调用javascript函数。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有一个带有常见javascript函数的4个asp文件。 现在我想将所有常用函数保存在其他文件中并从asp文件中调用该函数。 示例代码: <% - 由ASP2ASPX生成 4 。 5 。 ASP2ASPX主页:http: // www.netcoole.com Original文件名:C:\wwwroot \VWOnline \Sign.asp - %> <%@ Page language = c# 调试 = true %> <% string home = ; string flavor = ; %> <% Response.Buffer = true ; %> <% home = Request.QueryString [ Home]; flavor = Request.QueryString [ VWOFlavor]; %> <% if (Request.Form [ JreDloadHome ]!= ) { Session。添加( VWOViewer, 0 ) ; Session.Add( strHome,Request.Form [ Home]); Session.Add( JreDloadHome,Request.Form [ JreDloadHome]); Session.Add( VWOPDloadHome,Request.Form [ VWOPDloadHome]); Session.Add( VWOFDloadHome,Request.Form [ VWOFDloadHome]); if (Request.Form [ VWOFlavor ] == Lite) { Session .Add( VWOViewer, 1 ); Response.Redirect( VWOnLite.aspx); } if (Request.Form [ VWOFlavor] == 加) { Response.Redirect( VWPlus.aspx); } if (Request.Form [ VWOFlavor] == 完整) { Response.Redirect( VWApp.aspx); } } %> < HTML > < HEAD > < TITLE > 本地检查< / TITLE > < / HEAD > < BODY > < SCRIPT LANGUAGE = JavaScript1.3 > var ViewWiseOnlineScriptVersion = 300 ; var Netscape6 = false ; var IE5 = false ; var ViewWiseOnlineHome = ; var ViewWiseOnlineObjVersion = 0 ; var jreDownloadLink = ; var vwoFDownloadLink = ; var vwoPDownloadLink = ; var vwoFlavor = Lite; var browserType = ; window .onload = initViewWiseOnline; function initViewWiseOnline() { prepareDownloadLink(); WhoIsBrowsing(); if (!(Netscape6 | IE5)) { alert( 浏览器不支持); return ; } isInstalledVwo(); DisplayOptions(); } function prepareDownloadLink() { VWOurl = window . document .URL; index = VWOurl.lastIndexOf( /) VWOurl = VWOurl。 substr( 0 ,index); jreDownloadLink = VWOurl + / dload / jre-6u45-windows-i586.exe; vwoFDownloadLink = VWOurl + / dload / ViewWiseOnline.exe; vwoPDownloadLink = VWOurl + / dload / ViewWiseOnlinePlus.exe; } function WhoIsBrowsing() { var BrowserInfo = navigator .userAgent; var MozillaIndex = BrowserInfo.indexOf( 的Mozilla /); var MSIEIndex = BrowserInfo.indexOf( MSIE); if (MSIEIndex!= -1) { browserType = IE; var IEVersion = BrowserInfo.substr(MSIEIndex + 5, 2 ); if (IEVersion.indexOf( )。 )!= -1){ IEVersion = IEVersion.substr( 0 , 1 ); } 如果(IEVersion> = 5 )IE5 = 真; } else if (MozillaIndex!= -1) { browserType = Mozilla; var NSVersion = BrowserInfo.substr(MozillaIndex + 8, 1 ); if (NSVersion> = 5 )Netscape6 = 真; // Mozilla Brow。会工作! } } function isInstalledVwo() { 尝试 { 如果(IE5) { // alert(Calling Activex); var obj = new ActiveXObject( CVOOCX); ViewWiseOnlineObjVersion = obj.Version; if (ViewWiseOnlineScriptVersion> ViewWiseOnlineObjVersion) return (假); ViewWiseOnlineHome = obj.Home; // alert(ViewWiseOnlineHome); if (ViewWiseOnlineHome == ){ ViewWiseOnlineHome = <%= home%>; } // alert(vwoFlavor); vwoFlavor = obj.Flavor; if (vwoFlavor == ){ vwoFlavor = <%= flavor%>; } return ( true ); } 如果(Netscape6) { // ViewWiseOnlineHome =C:\Program Files \ ViewWise Online;和vwoFlavour =完整 ViewWiseOnlineHome = <%= home%>; vwoFlavor = <%= flavor%>; return ( true ); netscape.security.PrivilegeManager.enablePrivilege( UniversalXPConnect); var obj = Components.classes [ @ computhink.org / VWO; 1\" ]的createInstance(); obj = obj.QueryInterface(Components.interfaces.coIVWO); ViewWiseOnlineObjVersion = obj.vwoVersion; if (ViewWiseOnlineScriptVersion> ViewWiseOnlineObjVersion) return (假); ViewWiseOnlineHome = obj.vwoHome; vwoFlavor = obj.vwoFlavor; return ( true ); } } catch (e) { if (e == enablePrivilege未授予) { document .writeln(' < P>此网站需要扩展权限。< / P>'); document .writeln(' < P> ;您需要添加以下行:< / P>'); document .writeln(' < P> ;< STRONG> user_pref(signed.applets.codebase_principal_support,true);< / STRONG>'); document .writeln(' < P> ;到Mozilla用户个人资料目录中的prefs.js文件或< / P>'); document .writeln(' < P> ;< STRONG> pref< / STRONG>目录中的all.js文件然后重新启动Netscape。< / P>'); } return ( false ); } } 功能 DisplayOptions() { var tmpViewWiseOnlineHome = ViewWiseOnlineHome; var tmpjreDownloadLink = jreDownloadLink; var tmpvwoPDownloadLink = vwoPDownloadLink; var tmpvwoFDownloadLink = vwoFDownloadLink; var tmpvwoFlavor = vwoFlavor; // document.write('< FORM name =frmloginaction =Sign.aspx method = post>'); // document.write('< input NAME = Home type = hidden />'); // document.write(' < input NAME = JreDloadHome type = hidden />'); // document .write('< input NAME = VWOPDloadHome type = hidden />'); // document.write('< input NAME = VWOFDloadHome type = hidden />'); // document.write('< input NAME = VWOFlavor type = hidden />'); // document.write('< / FORM>'); document .frmlogin.VWOPDloadHome.value = tmpvwoPDownloadLink; document .frmlogin.VWOFDloadHome.value = tmpvwoFDownloadLink; document .frmlogin.JreDloadHome.value = tmpjreDownloadLink; document .frmlogin.Home.value = tmpViewWiseOnlineHome; document .frmlogin.VWOFlavor.value = tmpvwoFlavor; document .frmlogin.BrowserType.value = browserType; document .frmlogin.submit(); } < / SCRIPT > < FORM name = frmlogin action = Sign.aspx method = post> < input NAME = Home type = hidden /> < input NAME = JreDloadHome type = hidden /> < input NAME = VWOPDloadHome type = hidden /> < input NAME = VWOFDloadHome type = hidden /> < input NAME = VWOFlavor type = hidden /> < input NAME = BrowserType type = hidden /> < / 表格 > < / BODY > < / HTML > 解决方案 在您的网站上创建一个 .js文件在你要使用此文件中的任何函数的页眉中添加项目并添加对它的引用。 喜欢这个。 < head > < script src = ../../ Project / javascripts / Common.js 类型 = text / javascript > < / script > ; < / head > 进一步创建 .js文件,转到项目层次结构中的添加新项选项(右键单击解决方案资源管理器中的项目名称)并选择 JScript文件选项。 希望它会有所帮助。 :) 创建 javascript file 和在 site.master页面中将其命名为如下所示 <%:Scripts.Render( 〜/脚本/ yourscript.js)%> 您可以在 页面中调用 -keyword> 你可以调用任何 函数 来自该页 任何页面 在单个JS文件中添加所有功能。将此文件添加到页面的head部分之后,您可以使用该JS文件中的可用功能。 I have an 4 asp file with common javascript function.Now i want to keep all the common function in other file and call that function from asp files.sample code: <%-- Generated by ASP2ASPX 4.5. ASP2ASPX home page: http://www.netcoole.com Original File Name: C:\wwwroot\VWOnline\Sign.asp--%><%@ Page language="c#" Debug="true"%><% string home = ""; string flavour = "";%><% Response.Buffer = true;%><% home = Request.QueryString["Home"]; flavour = Request.QueryString["VWOFlavor"];%><% if (Request.Form["JreDloadHome"] != "") { Session.Add("VWOViewer", 0); Session.Add("strHome", Request.Form["Home"]); Session.Add("JreDloadHome", Request.Form["JreDloadHome"]); Session.Add("VWOPDloadHome", Request.Form["VWOPDloadHome"]); Session.Add("VWOFDloadHome", Request.Form["VWOFDloadHome"]); if (Request.Form["VWOFlavor"] == "Lite") { Session.Add("VWOViewer", 1); Response.Redirect("VWOnLite.aspx"); } if (Request.Form["VWOFlavor"] == "Plus") { Response.Redirect("VWPlus.aspx"); } if (Request.Form["VWOFlavor"] == "Full") { Response.Redirect("VWApp.aspx"); } }%><HTML><HEAD><TITLE>Local Check</TITLE></HEAD><BODY><SCRIPT LANGUAGE="JavaScript1.3">var ViewWiseOnlineScriptVersion = 300;var Netscape6 = false;var IE5 = false;var ViewWiseOnlineHome="";var ViewWiseOnlineObjVersion=0;var jreDownloadLink="";var vwoFDownloadLink="";var vwoPDownloadLink="";var vwoFlavor="Lite";var browserType="";window.onload = initViewWiseOnline;function initViewWiseOnline(){ prepareDownloadLink(); WhoIsBrowsing(); if (!(Netscape6 | IE5)) { alert("Browser not supported"); return; } isInstalledVwo(); DisplayOptions();}function prepareDownloadLink(){ VWOurl= window.document.URL; index = VWOurl.lastIndexOf("/") VWOurl = VWOurl.substr(0,index); jreDownloadLink = VWOurl+"/dload/jre-6u45-windows-i586.exe"; vwoFDownloadLink = VWOurl+"/dload/ViewWiseOnline.exe"; vwoPDownloadLink = VWOurl+"/dload/ViewWiseOnlinePlus.exe";}function WhoIsBrowsing(){ var BrowserInfo = navigator.userAgent; var MozillaIndex = BrowserInfo.indexOf("Mozilla/"); var MSIEIndex = BrowserInfo.indexOf("MSIE "); if (MSIEIndex != -1) { browserType = "IE"; var IEVersion = BrowserInfo.substr(MSIEIndex+5,2); if(IEVersion.indexOf(".") != -1){ IEVersion = IEVersion.substr(0,1); } if (IEVersion >= 5) IE5 = true; } else if (MozillaIndex != -1) { browserType = "Mozilla"; var NSVersion = BrowserInfo.substr(MozillaIndex+8,1); if (NSVersion >= 5) Netscape6 = true; //Mozilla Brow. will work! }}function isInstalledVwo(){ try { if (IE5) { //alert("Calling Activex"); var obj = new ActiveXObject("CVOOCX"); ViewWiseOnlineObjVersion = obj.Version; if (ViewWiseOnlineScriptVersion > ViewWiseOnlineObjVersion ) return(false); ViewWiseOnlineHome = obj.Home; //alert(ViewWiseOnlineHome); if(ViewWiseOnlineHome == ""){ ViewWiseOnlineHome = "<%= home %>"; } //alert(vwoFlavor); vwoFlavor = obj.Flavor; if(vwoFlavor == ""){ vwoFlavor = "<%= flavour %>"; } return(true); } if (Netscape6) { // ViewWiseOnlineHome = "C:\Program Files\ViewWise Online"; and vwoFlavour = full ViewWiseOnlineHome = "<%= home %>"; vwoFlavor = "<%= flavour %>"; return(true); netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); var obj = Components.classes["@computhink.org/VWO;1"].createInstance(); obj = obj.QueryInterface(Components.interfaces.coIVWO); ViewWiseOnlineObjVersion = obj.vwoVersion; if (ViewWiseOnlineScriptVersion > ViewWiseOnlineObjVersion) return(false); ViewWiseOnlineHome = obj.vwoHome; vwoFlavor = obj.vwoFlavor; return(true); } } catch (e) { if (e == "enablePrivilege not granted") { document.writeln('<P> This site requires extended privileges. </P>'); document.writeln('<P> You need to add the following line:</P> '); document.writeln('<P> <STRONG> user_pref("signed.applets.codebase_principal_support", true); </STRONG>'); document.writeln('<P> to the prefs.js file in Mozilla user profile directory or to the </P>'); document.writeln('<P> all.js file in the <STRONG>pref</STRONG> directory then restart Netscape. </P>'); } return(false); }}function DisplayOptions(){ var tmpViewWiseOnlineHome = ViewWiseOnlineHome; var tmpjreDownloadLink = jreDownloadLink; var tmpvwoPDownloadLink = vwoPDownloadLink; var tmpvwoFDownloadLink = vwoFDownloadLink; var tmpvwoFlavor = vwoFlavor; //document.write('<FORM name="frmlogin" action="Sign.aspx" method=post>'); //document.write('<input NAME=Home type= hidden />'); //document.write('<input NAME=JreDloadHome type= hidden />'); //document.write('<input NAME=VWOPDloadHome type= hidden />'); //document.write('<input NAME=VWOFDloadHome type= hidden />'); //document.write('<input NAME=VWOFlavor type= hidden />'); //document.write('</FORM>'); document.frmlogin.VWOPDloadHome.value = tmpvwoPDownloadLink; document.frmlogin.VWOFDloadHome.value = tmpvwoFDownloadLink; document.frmlogin.JreDloadHome.value = tmpjreDownloadLink; document.frmlogin.Home.value = tmpViewWiseOnlineHome; document.frmlogin.VWOFlavor.value = tmpvwoFlavor; document.frmlogin.BrowserType.value = browserType; document.frmlogin.submit();}</SCRIPT><FORM name="frmlogin" action="Sign.aspx" method=post> <input NAME=Home type= hidden /> <input NAME=JreDloadHome type= hidden /> <input NAME=VWOPDloadHome type= hidden /> <input NAME=VWOFDloadHome type= hidden /> <input NAME=VWOFlavor type= hidden /> <input NAME=BrowserType type= hidden /></FORM></BODY></HTML> 解决方案 create a .js file in your project and add reference to it in the page header where you want to use any of the function from this file.like this.<head><script src="../../Project/javascripts/Common.js" type="text/javascript"></script></head>Further to create a .js file , go to Add new Item option from project hierarchy(Right Click on Project Name in solution Explorer) and select JScript File option.Hope it will help. :)create a javascript file outside and call it in site.master page as shown below<%: Scripts.Render("~/Scripts/yourscript.js")%>which you can call in any page that is you can call any function from that page in any pageAdd all function in single JS file. add this file into head section of page after that you can use the function available in that JS file. 这篇关于如何从外部文件调用javascript函数。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-24 17:52