Firstly let me preface this by saying I would never WANT to do this, nor do I intend to do this, I simply need to know if it is possible.
首先让我先说这个我永远不想这样做,我也不打算这样做,我只需要知道它是否可行。
Considering that you can place "javascript:myFunction();" in a link's href attribute, is it possible to specify "javascript:myFunction();" as the url parameter in jQuery's $.ajax function?
考虑到你可以放置“javascript:myFunction();”在链接的href属性中,是否可以指定“javascript:myFunction();”作为jQuery的$ .ajax函数中的url参数?
Again, I know this would be counterintuitive and a pointless exercise for any production code. I am simply interested in whether it could theoretically be done.
同样,我知道这对于任何生产代码都是违反直觉和毫无意义的练习。我只是对理论上是否可以完成感兴趣。
4 个解决方案
#1
0
No it does not work.
不,它不起作用。
The javascript:myFunction()
is something of a hack URL that you can put as the href
of an anchor to link to a JS function.
javascript:myFunction()是一个黑客URL,可以作为锚点的href链接到JS函数。
$.ajax
wants to make an HTTP request and will attempt to, with whatever URL you provide. In the case of a javascript:*
URL, it will do nothing useful.
$ .ajax想要发出HTTP请求,并尝试使用您提供的任何URL。对于javascript:* URL,它不会有任何用处。
#2
0
You're free to pass that to "$.ajax()" as the URL, but it won't work. It's really an ugly old practice to use "javascript:" values for anchor tag "href" values too.
您可以将其作为URL传递给“$ .ajax()”,但它不起作用。对于锚标签“href”值也使用“javascript:”值这是一个非常丑陋的旧做法。
#3
0
No this will not work as intended. The A element is special case, and using that string with jQ.ajax() will result in a bad URL.
不,这不会按预期工作。 A元素是特殊情况,使用带有jQ.ajax()的字符串将导致错误的URL。
#4
0
You can put this in the url but the "javascript:" will becomes part of url, so you may get error that the url path can't be found as when browser transmit the request, it won't know that this is javascript.
您可以将其放在网址中,但“javascript:”将成为网址的一部分,因此当浏览器传输请求时,您可能会收到无法找到网址路径的错误,因此不会知道这是javascript。
#1
0
No it does not work.
不,它不起作用。
The javascript:myFunction()
is something of a hack URL that you can put as the href
of an anchor to link to a JS function.
javascript:myFunction()是一个黑客URL,可以作为锚点的href链接到JS函数。
$.ajax
wants to make an HTTP request and will attempt to, with whatever URL you provide. In the case of a javascript:*
URL, it will do nothing useful.
$ .ajax想要发出HTTP请求,并尝试使用您提供的任何URL。对于javascript:* URL,它不会有任何用处。
#2
0
You're free to pass that to "$.ajax()" as the URL, but it won't work. It's really an ugly old practice to use "javascript:" values for anchor tag "href" values too.
您可以将其作为URL传递给“$ .ajax()”,但它不起作用。对于锚标签“href”值也使用“javascript:”值这是一个非常丑陋的旧做法。
#3
0
No this will not work as intended. The A element is special case, and using that string with jQ.ajax() will result in a bad URL.
不,这不会按预期工作。 A元素是特殊情况,使用带有jQ.ajax()的字符串将导致错误的URL。
#4
0
You can put this in the url but the "javascript:" will becomes part of url, so you may get error that the url path can't be found as when browser transmit the request, it won't know that this is javascript.
您可以将其放在网址中,但“javascript:”将成为网址的一部分,因此当浏览器传输请求时,您可能会收到无法找到网址路径的错误,因此不会知道这是javascript。