JayTh
JayTh

Reputation: 17

How to hit url in javascript

I want to just hit the url in javascript which will run in background not on frontend.

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.

Upvotes: 0

Views: 4406

Answers (2)

Vnuuk
Vnuuk

Reputation: 6547

I suggest you use either jQuery or AngularJs for sending AJAX requests rather than creating new ActiveXObject objects.

Girish gave you answer using pure javascript. I will give you an example of jQuery usage. Please see code below:

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

Upvotes: 1

Girish
Girish

Reputation: 12127

you should use cross browser JavaScript AJAX code, or better to use jQuery ajax see below sample code

    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();

Upvotes: 2

Related Questions