0

I want to load a new page when I click one of the menu from sidebar. But the JavaScript load function doesn't work.

$(document).ready(function(){
  $( 'button' ).click(function() {
    $('#load').load('page1.html');
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="load"></div>
<button>Click to load a new page</button>

Please need help.

3
  • What does it says? What is the error? Commented Jan 3, 2018 at 4:09
  • The error is " Failed to load file:///C:/Users/HP/Desktop/SenCare/demo/neckPain.html: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https. " Commented Jan 3, 2018 at 4:22
  • Try absolute url instead page1.html. Commented Jan 3, 2018 at 4:30

5 Answers 5

1

Your Jquery code is correct. check in console error i think your page1.html is not found.

So, set correct page1.html file path.

If Page1.html path is correct then try this. it's work for me in chrome.

$(function() {
    $( 'button' ).on('click', function() {
        $('#load').load('page1.html');
    });
});

Html code.

<div id="load"></div>
<button>Click me</button>
Sign up to request clarification or add additional context in comments.

1 Comment

I have correctly give the file path TYPO3 Developer. But another interesting matter is that...load function is working fine in firefox but not in google crome. Is there any kind of browser issue for the load function?
0

I have moved javascript code to the end of page and it works:

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>load demo</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>
<body>

<div id="load"></div>
<button>Click to load a new page</button>
<script>
   $('button').click(function() {
        $('#load').load('page1.html');
    });
</script>
</body>
</html>

Comments

0

Try This

$(document).ready(function(){
  $( 'button' ).on('click', function() {
    $('#load').load('page1.html');
  });
});

Add button type button to your button tag

<button type="button">Click to load a new page</button>

Comments

0

A couple of things -

  1. Place your JS after your HTML to ensure all elements with the referenced IDs are loaded, or your code may not work.

  2. You don't even need JQuery to do what you want. See the following snippet which uses the fetch() API built into all modern browsers (note you'll get an error displayed if you run the snippet below on this page because page1.html isn't available on StackOverflow)"

<html>
	<head>
		<title>Hello</title>
	</head>
	<body>
		<div id="load" ></div>
		<button id="clickme">Click to load a new page</button>
		<script type="text/javascript">
			(function() {
	document.getElementById("clickme").onclick = function() {
		fetch("page1.html")
			.then(function(response) {
				response.text().then(function (text) {
					document.getElementById("load").innerHTML = text;
				})
			})
			.catch(function(e) {
				document.getElementById("load").innerHTML = `Error loading: ${e.message}`;
			})
	}
}());
		</script>
	</body>
</html>

Comments

0

Try to install any web server and try it.. it must work

1 Comment

already tried into web server before posting here the problem. but didn't work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.