将 URL 传递给 JS 函数

发布于 2024-11-16 20:25:47 字数 477 浏览 0 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

半透明的墙 2024-11-23 20:25:47

有许多不同的方法可以做到这一点,但通常很简单:

<script type="text/javascript">var myURL = '<?php echo $url; ?>'</script>

其中 $url 是您希望他们访问的 url(可能是 '/boxes.php?uid='.$_SESSION['id'] ;?)
这将转换为:

<script type="text/javascript">var myURL = 'http://whatever.and.stuff'</script>

编辑

您刚刚询问如何在外部 JS 页面上声明一个函数并使用该变量。

好吧,幸运的是,JS 并不关心你什么时候声明变量,只要它在你使用它之前具有一些值即可。而且,如果它没有值,它将提供 undefined;。这意味着您可以声明依赖于全局变量的函数,而这些变量在另外三秒或几秒/分钟/年/亿万秒/任何时间内都不会存在。

此外,页面上的脚本标签越早,它就越早被评估——当第二个js文件加载时,第一个标签中声明的变量将存在。

因此,如果您想按预期完成上述工作,您有两种选择。

您可以将变量定义为页面正文中的第一个脚本标记(因此,它是在 checkLaunchpad 之前定义的。

<script type="text/javascript">var myURL = '<?php echo $url; ?>'</script>
<script type="text/javascript" src="path/to/js"></script>

或者,如果 checkLaunchpad 直到之后才被调用正文已加载,您可以将定义放置在脚本标记中的任何位置!

There are a number of different ways to do it, but often it is as simple as:

<script type="text/javascript">var myURL = '<?php echo $url; ?>'</script>

Where $url is the url you want them to go to (perhaps '/boxes.php?uid='.$_SESSION['id'];?)
That will translate to:

<script type="text/javascript">var myURL = 'http://whatever.and.stuff'</script>

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.

<script type="text/javascript">var myURL = '<?php echo $url; ?>'</script>
<script type="text/javascript" src="path/to/js"></script>

Or, if checkLaunchpad isn't called until after the body has loaded, you can place the definition anywhere in a script tag!

只为一人 2024-11-23 20:25:47

查找以下代码将用户重定向到新页面

<script type="text/javascript">

var endURL = "http://google.com";

function checkLaunchpad(url) {    
     location.href=url;
     return false;      
}
</script>

<a href="javascript: void('0')" onclick="javascript: return checkLaunchpad(endURL);">Test</a>

以获取更多参考检查 http://snook.ca/archives/javascript/全局变量

Find below code to redirect users to new page

<script type="text/javascript">

var endURL = "http://google.com";

function checkLaunchpad(url) {    
     location.href=url;
     return false;      
}
</script>

<a href="javascript: void('0')" onclick="javascript: return checkLaunchpad(endURL);">Test</a>

for more reference check http://snook.ca/archives/javascript/global_variable

魂ガ小子 2024-11-23 20:25:47

我想说将 url 存储在隐藏字段中,例如:

<input id="hidUrl" type="hidden" value="<?php echo $url ?>"/>

然后更改您的 javascript,以便它使用该值:

window.location = $("#hidUrl").val();

I would say store the url in a hidden field like:

<input id="hidUrl" type="hidden" value="<?php echo $url ?>"/>

And then change your javascript so it uses the value:

window.location = $("#hidUrl").val();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文