Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
339 views
in Technique[技术] by (71.8m points)

html - Link that opens a NEW page and runs 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...