开发者

Using jQuery ajax to call Javascript function

开发者 https://www.devze.com 2023-03-07 19:46 出处:网络
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.

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?

Again, I know this would be counterintuitive and a pointless exerci开发者_如何学编程se for any production code. I am simply interested in whether it could theoretically be done.


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.

$.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.


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消