HI there,
I was again watching a jquery/ajax tutorial and in the video http://blog.themeforest.net/tutorials/jquery-for-absolute-beginners-day-10/ they show how to use a return:false; to determine whether a scrip will execute (javascript on) or will go to a separate page if the javascript is off.
Basically I have 2 pages test.htm and new_file.htm. The former will get the list from the latter page and load onto the test.htm. This is done via jquery. the trainer uses as a trigger a link on the test.htm page which originally goes nowhere, then he changes that to point to new-file.htm and he says and shows that if we add

return:false;

to the jquery script if you click on "show list" when javascript is enabled it works fine, when instead javascript is disabled when the link is clicked on it goes to the new_file.htm. I tried that in here http://antobbo.webspace.virginmedia.com/various_tests/2011_10_29_ajax/test.htm and obviously it doesn't work, whether javascript is on or off it still goes to the new_file.htm page so I was wondering if anybody can shed some light on it at all. Let's have a look at the code, it might be easier to understand rather than me attempting to explain:

test page:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
	<head>

		<title>test page</title>
		<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
		<script type="text/javascript">
			$(function(){
				$('a').click(function(){
				$('<div id="info"/>').load('new_file.htm #movies',function(){
				$(this).hide()
					.appendTo('#container')
					.slideDown(1000);

				});
					return:false;
			  });

			});
		</script>

	</head>


	<body>
		<div id="container">
			<h1>My movies</h1>
		</div>
		<a href="new_file.htm">Show list</a>
	</body>


</html>

page where the list is copied from:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
	<head>
		<title>another test page</title>
	</head>

	<body>
		<ul id="movies">
			<li>element 1</li>
			<li>element 2</li>
			<li>element 3</li>
		</ul>
	</body>
</html>

Never saw 'return:false' before. Did you mean 'return false' (no colon) or is that something I need to learn about?

First of all your subject has made it sound so wrong.. 'Jquery to determine javascript off' !!. Jquery itself is javascript, and you can't run a javascript code to check if javascript is enabled :) :)

Secondly i think the following e.g might help u understand the behaviour of return false;

<html>
<head>
<title>Jquery makes my day !</title>
</head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
	$("a.confirm").click(function(){
		var c = confirm('You are being redirected to '+$(this).attr('href')+'. Are you sure you want to navigate away ?');
		return c;
		/*if user clicks on 'OK', true is returned. If 'Cancel' is clicked, false is returned*/
	});
	$("a.disabled").click(function(){
		return false;
	});
});

</script>
<body>


<a href="http://google.com" class="confirm">confirm > go</a><br/>
<a href="http://images.google.co.in"> go directly</a><br/>
<a href="http://gmail.com" class="disabled">never load this link's url</a>

</body>
</html>

Hi ckchaudhary,
thanks for your reply. Yes your example is clear although there are few things that I don't understand. Now, return:false; from what I understand, seems to be used to cancel an action, like in your code the third link has a return:false; so the link won't work. Specifically in your code you say return c; but is it necessary? I mean if I remove it, the script still works fine.
I also tried one thing: I replaced your return c; with return:false; and noticed that the script still run in that the alert box is still fired but the link doesn't go anywhere. SO the return:false; acts on what the link is supposed to do, but not on the script (sorry I didn't know that!:-))

To go back to my code: sorry for the title of the post, wasn't quite sure how to call it.
I am not quite sure why in the video it works fine but when I do it (and literally I copied it) it doesn't. So, let me see if I understand correctly what the code I posted does then.
If javascript is enabled, when you click on the show list link, even if the link target is new_file.htm it should do what the jquery script asks, which is append a div with a list to the current page. (but it doesn't do it, it goes to the target page). Now in my case the return:false; (from what I understand from your example) should cancel the default action of the link (which is the link going to a new page) and should instead append the div.

If javascript is off instead, then the browser won't read the script at all therefore it will go to new_file.htm.
Is my interpretation correct?

The script replaces the original link of anchor tag ('new_file.htm') with movie link. If Javascript is off, the script will not replace the link -- the original link is used.

Thanks Taywin,
but why is it not working then? I don't know if you tried the link but it doesn't work the way it should. If the javascript is on or off it goes to a different page.

@MartinRinehart, sorry you're right, I meant return false; , apologies

When JavaScript encounters an error, it stops working right where it found the error and roll back all the changes. As a result, nothing seems to work. If JavaScript is turned off by the browser, nothing is even started in the first place. If the script is not doing what you are expecting (replace the link), it will not work again. :) In any case, the link remains as its original.

Hi there, right so there must be an error in my code then. I went through it again but i don't seem to be able to see anything wrong with it...any idea?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.