打开新页面并运行 javascript 的链接 [英] Link that opens a NEW page and runs javascript

查看:32
本文介绍了打开新页面并运行 javascript 的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何打开一个页面,然后在该页面上运行 javascript(由第一页上的链接设置).我试过这个: 在此处链接显示名称有谁知道如何实现这一目标?

I would like to know how to open a page, and then on that page, run javascript (that is set by the link on the first page). I have tried this: <a href = "http://www.example.com/otherpage" onclick = "javascript_on_otherpage()">LINK DISPLAY NAME HERE</a> Does anyone know how to achieve this?

推荐答案

如果新页面是你控制的页面,那么你可以通过链接向新页面传递一个查询参数,你可以在新页面检查该查询参数,如果找到,则在页面加载时在页面中运行一些代码.

If the new page is a page whose code you control, then you can pass a query parameter to the new page via the link and you can have the javascript in the new page check for that query parameter and, if found, run some code in the page upon page load.

<a href = "http://www.example.com/otherpage?runOnStartup=3">LINK DISPLAY NAME HERE</a>

新页面中的启动 javascript 将检查 runOnStartup 查询参数并根据其值运行一些代码.

And the startup javascript in the new page would check for the runOnStartup query parameter and run some code based on its value.

如果新页面与您的当前页面在同一来源,那么您可以在新窗口中打开新页面,然后在打开后,您可以在该新窗口中调用一个函数.但是,您之前的页面必须仍在运行才能执行此操作.

If the new page is in the same origin as your current page, then you could open the new page in a new window and then after it opened, you could call a function in that new window. But, your previous page would have to still be running in order to do that.

如果新页面来自不同的来源,并且您无法控制该新页面中的代码,那么出于浏览器安全原因,您将无法执行您所要求的操作.

If the new page is in a different origin and you do not control the code in that new page, then you cannot do what you're asking for browser security reasons.

这篇关于打开新页面并运行 javascript 的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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