简体   繁体   English

如何在JavaScript中点击网址

[英]How to hit url in javascript

I want to just hit the url in javascript which will run in background not on frontend. 我只想点击javascript中的网址,它将在后台而不是在前端运行。

I had tried this code 我已经尝试过此代码

var url = "http://yourpage.com";

req = new ActiveXObject("Microsoft.XMLHTTP");

   req.open("GET", true);

   req.onreadystatechange = callback;

   req.send(null);

but it not work for me. 但这对我不起作用。

could any one please suggest me how to hit url in javascript. 谁能建议我如何在javascript中打URL。

you should use cross browser JavaScript AJAX code, or better to use jQuery ajax see below sample code 您应该使用跨浏览器的JavaScript AJAX代码,或者更好地使用jQuery ajax,请参见下面的示例代码

    var url = "http://yourpage.com";
    var xmlhttp;

    if (window.XMLHttpRequest) {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    } else {
        // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange = callback;

    xmlhttp.open("GET", url, true);
    xmlhttp.send();

I suggest you use either jQuery or AngularJs for sending AJAX requests rather than creating new ActiveXObject objects. 我建议您使用jQueryAngularJs发送AJAX请求,而不要创建新的ActiveXObject对象。

Girish gave you answer using pure javascript. Girish使用纯JavaScript为您提供了答案。 I will give you an example of jQuery usage. 我将给您一个jQuery使用示例。 Please see code below: 请参见下面的代码:

$.get('http://yourpage.com', function(){ /*callback*/ })

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

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