Clint_A
Clint_A

Reputation: 536

Executing a url without opening it on the browser in Javascript / Jquery

I have a local page that pulls data from a database and sends out a message. So I'm trying to have this page executed using javascript on "success" of another function. The problem is everything I've tried doesn't seem to execute that page, while the only success I've had on executing that page is using a window pop up, which is not desired.

This is the pop up code (undesired):

function sendMsg(){
    var wnd = window.open("http://localhost/url");
    if(wnd){
            setTimeout(function () { wnd.close();}, 4000);
    }
}
sendMsg();

And these are the codes I've tried but didn't execute the url:

$.get("http://localhost/url")

And this one which is from another answer here on SO.

var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange=function() {
  if (xmlhttp.readyState==4 && xmlhttp.status==200) {
    var response = xmlhttp.responseText; returned value
  }
}

xmlhttp.open("GET","http://localhost/url",true);
xmlhttp.send();

How can I have this URL executed without opening it on the browser in any way? Just to be clear, I know the other two codes didn't work because I would have received a message.

Upvotes: 3

Views: 17937

Answers (2)

synz
synz

Reputation: 583

if you really need a return data/page from get

you should do something like this.

$.get( "url", function( data ) {
  console.log(data); // return data/page
  alert( "Done load." );
});

@ use ajax

$.ajax({
  url: "url",
  type: "GET",
  success   : function( data ){
    console.log(data); // return data/page
    alert("Done LOad");
  }
});

anything else can refer : https://api.jquery.com/jquery.get/

Upvotes: 4

iPzard
iPzard

Reputation: 2368

I believe jQuery's load() method may be useful for this. For example, have a div in your HTML and just set it to hidden in the CSS.

jQuery load() Method

HTML:

<div id='myHiddenPage'></div>

CSS:

#myHiddenPage { 
    display: none;
} 

jQUERY:

$(funtion() {
    $('#myHiddenPage').load('www.myurl.com');
});

Upvotes: 2

Related Questions