简体   繁体   English

使用jQuery ajax调用Javascript函数

[英]Using jQuery ajax to call Javascript function

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();" 考虑到您可以放置​​“ javascript:myFunction();” in a link's href attribute, is it possible to specify "javascript:myFunction();" 在链接的href属性中,是否可以指定“ javascript:myFunction();” as the url parameter in jQuery's $.ajax function? 作为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. 我只是对理论上是否可以实现感兴趣。

You're free to pass that to "$.ajax()" as the URL, but it won't work. 您可以随意将其作为URL传递给“ $ .ajax()”,但它将不起作用。 It's really an ugly old practice to use "javascript:" values for anchor tag "href" values too. 对于锚标记“ href”值也使用“ javascript:”值确实是一个丑陋的老做法。

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错误。

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()是一种hack URL,您可以将其作为锚的href放置以链接到JS函数。

$.ajax wants to make an HTTP request and will attempt to, with whatever URL you provide. $.ajax希望发出HTTP请求,并尝试使用您提供的任何URL。 In the case of a javascript:* URL, it will do nothing useful. 如果是javascript:* URL,它将无济于事。

您可以将其放在url中,但“ javascript:”将成为url的一部分,因此当浏览器传输请求时,您可能会收到无法找到url路径的错误,它不会知道这是javascript。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM