在.js文件中调用URL参数 [英] Calling URL parameters within a .js file

查看:179
本文介绍了在.js文件中调用URL参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在HTML文件中调用.js文件。在.js文件的URL上,我想要包含一个参数,该参数可以访问.js文件中的代码。

I am calling a .js file within a HTML file. On the URL to the .js file I want to include a parameter that will be accessable to the code INSIDE the .js file.

例如:

我希望能够在jQuery的帮助下将ID值传递给jquery_widget.js文件中的函数。这是怎么做到的?

I want to be able to pass the ID value to a function within the jquery_widget.js file, with the help of jQuery. How is this done?

感谢所有帮助!

推荐答案

你不能这样做:你必须在加载脚本之前声明变量。
示例:

You can't do it like that: you have to declare the variable before loading the script. Example:

<script type="text/javascript">
    var foo= "bar";
</script>
<script type="text/javascript" href="path/to/js.js"></script>

通过这种方式,变量将为您的脚本做好准备。

in this way, the variable will be ready for your script.

另一个解决方案是使用php脚本,然后可以使用GET变量。
在这种情况下,请确保通过header()调用告诉您正在输出javascript

Another solution is to use a php script, that can then use the GET variable. In that case, make sure you tell via a header() call that you are outputting javascript

<script type="text/javascript" src="ip.php"></script>

和ip.php

<?
//"ip.php" example- display user IP address on any page
Header("content-type: application/x-javascript");
$serverIP=$_SERVER['REMOTE_ADDR'];
echo "document.write(\"Your IP address is: <b>" . $serverIP . "</b>\")";
?>

这篇关于在.js文件中调用URL参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆