Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 832
Clint_A Avatar asked Feb 19 '26 09:02

Clint_A


2 Answers

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/

like image 118
synz Avatar answered Feb 21 '26 23:02

synz


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');
});
like image 31
iPzard Avatar answered Feb 21 '26 22:02

iPzard



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!