Help - Search - Members - Calendar
Full Version: redirection failed without jquery
HTMLHelp Forums > Programming > Client-side Scripting
jackmaessen
I have this html file which should download a .zip file and redirect to, per example, google.com
CODE

<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<!--<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>-->

<title>Download</title>

</head>
<body>

<a href="download.zip" id="download" target="_blank">Download file and redirect to google.com</a>

<script type="text/javascript">

$('#download').click(function() {
  window.location = 'http://www.google.com';
});

</script>

</body>
</html>

If i disable the jquery in the head, there will be no redirection to google.
I do not understand that.
It should still work even without the jquery?
Can someone explain me this?
What is the function of jquery?
pandy
The snip in the script block calls an event listener function in the jQuery file when the link with the id "download" is clicked and the window.location line runs, something like that anyway. Without the jQuery file that can't happen.

jQuery is what's called JavaScript library. A large file that contains a lot of canned JavaScript. Supposedly it makes it easier to use neat JS tricks than if you had to either write or find he individual scrips yourself. I'm no sure that is true. jQuery itself is pretty advanced JavaScript, so understand it by reading it directly is close to impossible if you aren't already pretty good at JavaScript. Instead you have to learn how to use jQuery, learn it's own syntax so to speak, without really knowing what you are doing. Just learn to push the right buttons. I lean towards that it's better to put that time on learning JS.

It's also overkill to load a large JavaScript library only to use a fraction of it, which is often the case. jQuery doesn't do anything that couldn't be done without it, only you need to either learn how to or find a (smaller) script that does what you want.
Christian J
Here's the equivalent with traditional java script:

CODE

<a href="download.zip" target="_blank" onclick="window.location = 'http://www.google.com';">Download file and redirect to google.com</a>
jackmaessen
you are right; i did not notice i was using jquery and not plain js
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2024 Invision Power Services, Inc.