-6

Is there any predefined function or any way to open a website url using javascript? as we do in php:

file_get_contents();

How can I open a link using javascript? Without using AJAX or JQuery or Flash

thecoshman
  • 8,394
  • 8
  • 55
  • 77
LIGHT
  • 5,604
  • 10
  • 35
  • 78
  • 3
    Without HTTP and servers as well? – Neil Oct 02 '12 at 10:00
  • May be using that, Actually, I don't know what that is. Could you please post the answer. I just want that done without importing any API. – LIGHT Oct 02 '12 at 10:01
  • Open a website meaning what? Opening a new link? Opening a new window? Get the html? Open an iframe on your site? – Undefined Oct 02 '12 at 10:04
  • I don't think it can be done without ajax at the least, but you don't need libraries to get that to work. Browsers either support it or they don't, and I think all modern browsers do. – Neil Oct 02 '12 at 10:05
  • Hmm.. Okay, I searched much in internet but didn't got the answer. so, still wondering in search of answer without ajax or javascript. – LIGHT Oct 02 '12 at 10:11
  • 1
    wait, now you want to do it without even using JavaScript? – thecoshman Oct 02 '12 at 10:22
  • @thecoshman, it is as I said: apparently without http and servers, keyboards, CPU, UTF-8, wires or cables. – Neil Oct 02 '12 at 12:09

2 Answers2

2

No, there is no way for JS to load content Asynchronously with out using, to a degree, AJAX. After all, loading Asynchronously is more or less the entire point of AJAX.

Personally, I would say you should just accept using JQuery, whilst it is not the perfect solution to all life's problems, it certainly does the job when it comes to AJAX requests like this.

EDIT

Though, that said, you may be simply wishing to simulate the use clicking a link... but that is a different question entirely.

thecoshman
  • 8,394
  • 8
  • 55
  • 77
  • 1
    +1. Used enough jQuery. – Neil Oct 02 '12 at 10:07
  • you can give other frameworks a try then. – midhunhk Oct 02 '12 at 10:10
  • +1 Anyone who doesn't want to use jQuery for AJAX is free to read the jQuery source and duplicate it. After all, jQuery is written in JavaScript without jQuery, an apparent secret that many people doesn't seem to realize. [I have already written](http://stackoverflow.com/questions/5099949/what-are-some-empirical-technical-reasons-not-to-use-jquery/5100169#5100169) about the fear of using jQuery so don't get me started... – rsp Oct 02 '12 at 10:12
  • 1
    @rsp If you learn how to use a buzzsaw without first learning how to saw a piece of wood by hand, you live with the fact that you have to carry that buzzsaw everywhere you'll need it. If that's not a problem, to each his own. I'm of the idea that you learn both thoroughly and use whatever is better suited depending on the situation. Not simply use one over the other always. Adaptability is a programmer's best instrument. – Neil Oct 02 '12 at 10:17
  • @Neil I would argue that it is more like using a swiss army knife without first learning how to cut a piece of wood using teeth and fingernails and living with the fact that you have to carry that swiss army knife everywhere you'll need it - this would be a more appropriate analogy in my experience. ;) Seriously though, I share your view that one should learn the language inside out even reinventing the wheel as you go just to understand it better, but at the end of the day one should use a tested solution in production. There are a lot of good libraries other than jQuery for that. – rsp Oct 02 '12 at 10:29
  • @SaintGerbil is this a genuine question, or are you just trolling? You cannot load a second page through JS without having first loaded the main page and the JS for it (of course not everything has to be loaded, but let's not get picky). Thus, it has to be done asynchronously... or are you getting at the fact you could do sequentially, rather then in parallel? if so, you are asking about something unrelated to this – thecoshman Oct 02 '12 at 10:52
  • Well I'm reading the question again and it doesn't mention async. I agree that the first page (and related scripts) must be loaded. Retrieving a file via Ajax cannot be downloaded as via javascript (due to security issues) http://stackoverflow.com/questions/4545311/how-to-download-a-file-by-jquery-ajax , I would have thought sending the user to page or popup a dialog would have been simpler (which I think you mean as sequentially) ? P.S. Interest not trolling. – Mark Broadhurst Oct 02 '12 at 12:29
  • 1
    It's very hard question to answer, as he is very unclear as to what exactly he meant. I took his meaning to be, he wanted to load the content of another page into this one, aka AJAX. I don't think he is trying to download a file through JS, nor do I think he simply wants to redirect the user to another page. – thecoshman Oct 03 '12 at 08:12
0

the simplest way to do this is :

window.location = "url to your document";

and make sure the server adds a header like this:

'Content-Disposition: attachment; filename="downloaded.pdf"'
Mark Broadhurst
  • 2,675
  • 23
  • 45