从 URL/地址栏调用 Javascript 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4163879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Call Javascript function from URL/address bar
提问by DazManCat
Is it possible to call a javascript function from the URL? I am basically trying to leverage JS methods in a page I don't have access to the source.
是否可以从 URL 调用 javascript 函数?我基本上是在尝试在我无法访问源代码的页面中利用 JS 方法。
Something like: http://www.example.com/mypage.aspx?javascript:printHelloWorld()
就像是: http://www.example.com/mypage.aspx?javascript:printHelloWorld()
I know if you put javascript:alert("Hello World");
into the address bar it will work.
我知道如果你javascript:alert("Hello World");
输入地址栏它会工作。
I suspect the answer to this is no but, just wondered if there was a way to do it.
我怀疑这个答案是否定的,只是想知道是否有办法做到这一点。
采纳答案by Nick Craver
There isn't from a hyperlink, no. Not unless the page has script inside specifically for this and it's checking for some parameter....but for your question, no, there's no built-in support in browsers for this.
没有来自超链接,不。除非页面内部有专门用于此的脚本并且它正在检查某些参数......但对于您的问题,不,浏览器中没有内置支持。
There are however bookmarkletsyou can bookmark to quickly run JavaScript functions from your address bar; not sure if that meets your needs, but it's as close as it gets.
然而有书签工具,你可以书签快速从地址栏运行JavaScript的功能; 不确定这是否满足您的需求,但它尽可能接近。
回答by hfarazm
Write in address bar
在地址栏中写入
javascript:alert("hi");
Make sure you write in the beginning: javascript:
确保你在开头写: javascript:
回答by yassine45
You can use Data URIs.
For example:
data:text/html,<script>alert('hi');</script>
您可以使用数据 URI。例如:
data:text/html,<script>alert('hi');</script>
For more information visit: https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs
更多信息请访问:https: //developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs
回答by Budia
/test.html#alert('heello')
/test.html#alert('你好')
test.html
<button onClick="eval(document.location.hash.substring(1))">do it</button>
回答by heximal
you may also place the followinng
您也可以放置以下内容
<a href='javascript:alert("hello world!");'>Click me</a>
to your html-code, and when you click on 'Click me' hyperlink, javascript will appear in url-bar and Alert dialog will show
到您的 html 代码,当您单击“单击我”超链接时,javascript 将出现在 url-bar 中,并且警报对话框将显示
回答by Eddy
About the window.location.hash
property:
Return the anchor part of a URL.
关于window.location.hash
属性:
返回 URL 的锚点部分。
Example 1:示例 1:
//Assume that the current URL is
var URL = "http://www.example.com/test.htm#part2";
var x = window.location.hash;
//The result of x will be:
x = "#part2"
Exmaple 2:
示例 2:
$(function(){
setTimeout(function(){
var id = document.location.hash;
$(id).click().blur();
}, 200);
})
Example 3:
示例 3:
var hash = "#search" || window.location.hash;
window.location.hash = hash;
switch(hash){
case "#search":
selectPanel("pnlSearch");
break;
case "#advsearch":
case "#admin":
}
回答by EllemKah
Using Eddy's answer worked very well as I had kind of the same problem. Just call your url with the parameters : "www.mypage.html#myAnchor"
使用 Eddy 的答案效果很好,因为我遇到了同样的问题。只需使用参数调用您的网址:“www.mypage.html#myAnchor”
Then, in mypage.html :
然后,在 mypage.html 中:
$(document).ready(function(){
var hash = window.location.hash;
if(hash.length > 0){
// your action with the hash
}
});
回答by T.Todua
you can use like this situation:
for example, you have a page: http://www.example.com/page.php
then in that page.php, insert this code:
您可以使用这种情况:例如,您有一个页面: http://www.example.com/page.php
然后在该 page.php 中插入以下代码:
if (!empty($_GET['doaction']) && $_GET['doaction'] == blabla ){
echo '<script>alert("hello");</script>';
}
then, whenever you visit this url: http://www.example.com/page.php?doaction=blabla
然后,每当您访问此网址时: http://www.example.com/page.php?doaction=blabla
then the alert will be automatically called.
然后将自动调用警报。