将 URL 传递给 JS 函数
我有一个 PHP 页面,我还使用 jQuery UI 功能。用户将项目从左侧面板拖到右侧的框中。放下后会出现一个对话框。如果用户单击第一个按钮,他/她将被重定向到一个 URL。我需要能够根据用户更改 URL。
我可以将 URL 放入 var 或 $_SESSION 中,但我不知道如何在 JS 函数中更改它?
这是DEMO(将项目放入框中以获取对话框)。
更新:
下面是所有有效的建议,但是,我的问题是我在外部 JS 文件(dragdrop-client.js)中拥有大部分 JS 函数。
我已将新的 var 添加到我的主 PHP 文件中
var endURL = "http://google.com";
,但如何让我的函数 checkLaunchpad() 使用它?
I have a PHP page with where I also use jQuery UI functionality. User drags Item from left panel into a box on the right. After it is dropped a dialog appears. If user clicks the first button he/she is redirected to a URL. I need to be able to change URL based on a user.
I can get the URL into a var or $_SESSION but I can't figure out how do I change it inside my JS function?
Here's the DEMO (drop item into box to get dialog).
UPDATE:
Below are all valid suggestions, however, my problem is that I have most of JS functions in external JS file (dragdrop-client.js).
I have added new var to my main PHP file
var endURL = "http://google.com";
but how do make my function checkLaunchpad() use it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有许多不同的方法可以做到这一点,但通常很简单:
其中 $url 是您希望他们访问的 url(可能是 '/boxes.php?uid='.$_SESSION['id'] ;?)
这将转换为:
编辑
您刚刚询问如何在外部 JS 页面上声明一个函数并使用该变量。
好吧,幸运的是,JS 并不关心你什么时候声明变量,只要它在你使用它之前具有一些值即可。而且,如果它没有值,它将提供
undefined
;。这意味着您可以声明依赖于全局变量的函数,而这些变量在另外三秒或几秒/分钟/年/亿万秒/任何时间内都不会存在。此外,页面上的脚本标签越早,它就越早被评估——当第二个js文件加载时,第一个标签中声明的变量将存在。
因此,如果您想按预期完成上述工作,您有两种选择。
您可以将变量定义为页面正文中的第一个脚本标记(因此,它是在
checkLaunchpad
之前定义的。或者,如果
checkLaunchpad
直到之后才被调用正文已加载,您可以将定义放置在脚本标记中的任何位置!There are a number of different ways to do it, but often it is as simple as:
Where $url is the url you want them to go to (perhaps '/boxes.php?uid='.$_SESSION['id'];?)
That will translate to:
EDIT
You just asked how you would have a function declared on an external JS page use the variable.
Well, fortunately, JS doesn't care when you declare the variable, so long as it has some value before you use it. And, if it doesn't have a value then, it will supply
undefined
;. This means you can declare functions which rely on global variables which won't exist for another three or for seconds/minutes/years/eons/whatever.Additionally, the earlier a script tag is on a page, the earlier it will be evaluated -- a variable declared in the first tag will exist when the second js file loads.
So, if you want to have the above work as expected you have two options.
You could define the variable as the first script tag in the body of the page (thus, it is defined before
checkLaunchpad
is.Or, if
checkLaunchpad
isn't called until after the body has loaded, you can place the definition anywhere in a script tag!查找以下代码将用户重定向到新页面
以获取更多参考检查 http://snook.ca/archives/javascript/全局变量
Find below code to redirect users to new page
for more reference check http://snook.ca/archives/javascript/global_variable
我想说将 url 存储在隐藏字段中,例如:
然后更改您的 javascript,以便它使用该值:
I would say store the url in a hidden field like:
And then change your javascript so it uses the value: