Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript & https - XMLHttpRequest object to GET relative path - is protocol/port 'inherited'?

If I use relative paths in Javascript to GET a page from a server (to display output inside a div), does Javascript use the same protocol/port as the page in which it was loaded?

For example:

parent page is requested https://www.foo.com/bar.php

JS code on bar.php:

var turl = "/new_dir/index.php?r="+r;
if(window.XMLHttpRequest){  
    xmlhttp=new XMLHttpRequest();
}else{
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET",turl,false);
xmlhttp.send(null);

Since the parent page was requested and served using https on port 443 does this mean that JS will send the GET request to the new page using the same protocol and port? Or will it send the request via http on port 80 since I did not specify a connection protocol in the 'turl' variable?

like image 719
mr.dancrane Avatar asked Feb 03 '11 15:02

mr.dancrane


People also ask

Whats JavaScript is used for?

JavaScript is a scripting language that enables you to create dynamically updating content, control multimedia, animate images, and pretty much everything else.

Is HTML and JavaScript same?

Both of these are computer languages that help in programming, but there is a major difference between JavaScript and HTML. While JavaScript (abbreviated as JS) is a scripting language, HTML is a markup language. We use HTML to create web pages or web applications.

Is JavaScript a free download?

For those want to learn to program, one of the biggest advantages of JavaScript is that it is all free.

Which is better JavaScript or HTML?

JavaScript simply adds dynamic content to websites to make them look good. HTML work on the look of the website without the interactive effects and all. HTML pages are static which means the content cannot be changed. It adds interactivity to web pages to make them look good.


1 Answers

It will use the same port and protocol, since you haven't specified anything else. Your URL is a relative reference in RFC-speak, details in Section 4.2 of the RFC. (I only happen to know the RFC section reference because I just recently found out about this nifty trick for http/https stuff.)

So your request for

/new_dir/index.php?r=blah

relative to the document

http://www.foo.com/bar.php

resolves to

http://www.foo.com/new_dir/index.php?r=blah
like image 175
T.J. Crowder Avatar answered Nov 02 '22 06:11

T.J. Crowder