简体   繁体   English

从另一个页面使用 url 调用 javascript function(带参数)的最佳(和安全)方式

[英]Best (and secure) way to call javascript function (with arguments) using a url from another page

After reviewing dozens of Stack Overflow posts, I'm thoroughly confused.在查看了数十篇 Stack Overflow 帖子后,我彻底困惑了。 What I am trying to do is create a URL through an tag on one page that would open another webpage and run a function that requires two arguments. I thought this would be simple but I keep seeing references to "cross site scripting vulnerabilities" and I am not familiar with this potential security problem and feel like I am now playing with fire.我想做的是通过一个页面上的标签创建一个 URL,这将打开另一个网页并运行一个 function,它需要两个 arguments。我认为这很简单,但我一直看到对“跨站点脚本漏洞”的引用,我我不熟悉这个潜在的安全问题,感觉我现在是在玩火。 I do not want to utilize something — even if the code works — if it opens up security risks.我不想使用某些东西——即使代码有效——如果它会带来安全风险。 Could someone point me in the right direction with the correct (and most secure) way to do this?有人能用正确的(也是最安全的)方法为我指明正确的方向吗? I can do my research (and learning) from there.我可以从那里进行研究(和学习)。 Much appreciated.非常感激。

For example you can append some parameter at the end of your URL https://your-url/?parameter=hello例如,您可以在 append URL https 的末尾添加一些参数://your-url/?parameter=hello

When this URL is opened on another webpage you can run JavaScript or a PHP function based on that URL query.当此 URL 在另一个网页上打开时,您可以运行 JavaScript 或 PHP function 基于该 URL 查询。

For JavaScript对于 JavaScript

 getUrlParam(slug) { let url = new URL(window.location); let params = new URLSearchParams(url.search); let param = params.get(slug); if (param) { return param; } else { return false; } } console.log(getUrlParam('parameter'));

After that, you can run this function to check if any parameter is passed in that URL or not.之后,您可以运行此 function 来检查是否在该 URL 中传递了任何参数。 If this function returns that's given slug parameter you can run your custom code inside that if condition block如果此 function 返回给定的 slug 参数,您可以在该 if 条件块内运行自定义代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM