Javascript 生成并使用URL作为jscript变量
我现在已经花了几个小时试图通过阅读其他人的帖子来弄清楚你是如何做到这一点的——我甚至有一个JSFIDLE可以使用,但在我的页面中无法使用它 我想构造一个在页面上多次使用的URL,这样当我需要更新URL时,我只需要通过页眉中的Javascript变量在一个地方进行更新 我将URL分为两部分,因为其中一个变量几乎总是相同的,但另一个变量在不同的页面上通常是不同的 例如,我在标题中声明:Javascript 生成并使用URL作为jscript变量,javascript,html,url,url-rewriting,createelement,Javascript,Html,Url,Url Rewriting,Createelement,我现在已经花了几个小时试图通过阅读其他人的帖子来弄清楚你是如何做到这一点的——我甚至有一个JSFIDLE可以使用,但在我的页面中无法使用它 我想构造一个在页面上多次使用的URL,这样当我需要更新URL时,我只需要通过页眉中的Javascript变量在一个地方进行更新 我将URL分为两部分,因为其中一个变量几乎总是相同的,但另一个变量在不同的页面上通常是不同的 例如,我在标题中声明: <script language="javascript" type=”text/jav
<script language="javascript" type=”text/javascript”>
function goSchedule()
{
var schedulePath = "http://[rootPath]/";
var scheduleFileName = "[extension to document].htm";
schedulePath = schedulePath + scheduleFileName;
document.getElementById('go').href= schedulePath;
}
</script>
函数goSchedule()
{
var schedulePath=“http://[rootPath]/”;
var scheduleFileName=“[document].htm扩展名”;
schedulePath=schedulePath+scheduleFileName;
document.getElementById('go')。href=schedulePath;
}
然后我似乎不知道如何在href中调用它。这不起作用:
<p>Click the following link to test:<a href="javascript:goSchedule();" id="go">Test this link</a></p>
单击以下链接进行测试:
如果您回答,请解释初始Javascript是如何创建的,以及如何正确调用它以使其成为活动URL。在用户单击链接时,您似乎正在尝试替换href属性。我建议您在页面加载完成后立即替换所有链接的href属性一次 确保您在head部分声明了您的函数
<head>
<!-- Other head declarations ... -->
<script language="javascript" type=”text/javascript”>
function goSchedule() {
var schedulePath = "http://[rootPath]/";
var scheduleFileName = "[extension to document].htm";
schedulePath = schedulePath + scheduleFileName;
document.getElementById('go').href = schedulePath;
}
</script>
</head>
函数goSchedule(){
var schedulePath=“http://[rootPath]/”;
var scheduleFileName=“[document].htm扩展名”;
schedulePath=schedulePath+scheduleFileName;
document.getElementById('go')。href=schedulePath;
}
然后将此方法绑定到body元素的onload事件,如下所示
<body onload="goSchedule()">
<!-- Other HTML Stuff Goes Here ... -->
<p>Click the following link to test:<a href="javascript:void" id="go">Test this link</a>
</p>
</body>
单击以下链接进行测试:
以下是完整的html页面:
<html>
<head>
<!-- Other head declarations ... -->
<script language="javascript" type="text/javascript">
function goSchedule() {
var schedulePath = "http://support.mozilla.org/";
var scheduleFileName = "en-US/products/firefox?as=u&utm_source=inproduct";
schedulePath = schedulePath + scheduleFileName;
document.getElementById('go').href = schedulePath;
}
</script>
</head>
<body onload="goSchedule()">
<!-- Other HTML Stuff Goes Here ... -->
<p>Click the following link to test:<a href="javascript:void" id="go">Test this link</a>
</p>
</body>
</html>
函数goSchedule(){
var schedulePath=”http://support.mozilla.org/";
var scheduleFileName=“en-US/products/firefox?as=u&utm\u source=inproduct”;
schedulePath=schedulePath+scheduleFileName;
document.getElementById('go')。href=schedulePath;
}
单击以下链接进行测试:
Hi Sebernimer,谢谢你。我用你的文本替换了我的文本,并用设置为。。。var schedulePath=“”;和var scheduleFileName=“en-US/products/firefox?as=u&utm\u source=inproduct”;结果不起作用。我还尝试了一个没有特殊字符的更简单的URL,但它也不起作用。你知道会发生什么吗-GeorgeAlso,我在Chrome、Firefox和IE(所有当前版本)中测试了这个页面。没有,我也包括了它-GeorgeThese是我在mozilla测试中使用的两条路径-我将在下一条评论中发布,这样你就可以完整地看到它了。-George function goSchedule(){var schedulePath=”“;var scheduleFileName=“en-US/products/firefox?as=u&utm_source=inproduct”;schedulePath=schedulePath+scheduleFileName;document.getElementById('go')。href=schedulePath;}