Load function in jQuery does not work - javascript

I am trying to load a header and footer file into jQuery, so I wrote the following code:
<html>
<head>
<title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>
$(function() {
$("#header").load("testheader.html");
$("#footer").load("testfooter.html");
});
</script>
</head>
<body>
<div id="header"></div>
<p> Hello World! </p>
<div id="footer"></div>
</body>
</html>
However, nothing loads from header and footer. I searched around StackOverflow and they talked about how if you import the wrong library you wouldn't get the load function properly. I tried to import different sources of jquery but nothing works. Please helpp!! Thanks!

Related

jquery works in external js file but not in view blade

I am using laravel 5.5. When i am using jquery in external js file it works but when i write jquery inside the blade view it gives an error: $ not defined or found. I also checked by writing jquery in the layout file after loading the app.js file, it works. Jquery is only not working inside the blade. Jquery works inside the blade when i load a jquery. But then how is it working in external js and layout even after not loading jquery. So my concern is if jquery is loaded by default in laravel why it is not working inside blade ?
Here is my blade content:
#extends('layouts.app')
#section('content')
<div class="container">
</div>
#endsection
<script type="text/javascript">
$(document).ready(function() {
alert('lol');
})
</script>
Your javascript code is outside any section, so it will not be rendered. A nice approach to rendering inline javascript in blade is to use #stack in your layout and then #push from any view extending the layout:
app.blade.php:
<html>
<head></head>
<body>
[...]
#yield('content')
[...]
<script src="jquery.js"></script>
#stack('scripts')
</body>
</html>
Your view (template.blade.php):
#extends('layouts.app')
#section('content')
<div class="container">
</div>
#endsection
#push('scripts')
<script type="text/javascript">
$(document).ready(function() {
alert('lol');
})
</script>
#endpush
Please, do understand:
This does work:
<html>
<header>
<script src="jquery.js"></script>
</header>
<body>
<script>
/* your js inline code here */
</script>
</body>
</html>
But a better solution is:
<html>
<header>
</header>
<body>
<script src="jquery.js"></script>
<script>
/* your js inline code here */
</script>
</body>
</html>
This will not work:
<html>
<header>
</header>
<body>
<script>
/* your js inline code here */
</script>
<script src="jquery.js"></script>
</body>
</html>

Separate HTML / JavaScript

I'm attempting to have my JavaScript code in a file apart from my HTML file.
Linking the 2 scripts between each other works, however stuff like the function document.getElementById() doesn't.
Anyone know how to fix this?
HTML code:
<!DOCTYPE HTML>
<html>
<head>
<title>Boost</title>
<script src="javascript.js"></script>
<link rel="stylesheet" type="text/css" href="styles.css"/>
</head>
<body>
<h3>
<p id="TopTextParagraph">Hi there</p>
<div>
<button id="clickNext">Next</button>
</div>
</h3>
</body>
</html>
JavaScript code:
document.getElementById("clickNext").onclick = goNext;
function goNext() {
console.log("stuff here")
}
JavaScript works when I put it in the HTML file using <script> </script>, just don't know how to make it work in a separate JavaScript file.
You are adding the script inside head tag. When that snippet is executed clickNext element does not exist in the dom
You can add the script near the closing body tag to resolve the problem & remove from the head tag
<body>
<h3>
<p id="TopTextParagraph"> Hi there</p>
<div>< button id="clickNext"> Next</button></div>
</h3>
<script src="javascript.js"></script>
</body>

call parital html page using jQuery

I want to call a partial html page using jquery. I have seen this
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script type="text/javascript" src="lib/jquery-3.1.0.min.js"></script>
<script type="text/javascript"> $('#result').load('Ajax/test.html'); </script>
<title>Home</title>
</head>
<body>
<div id="result"></div>
</body>
</html>
and in the path Ajax/test.html i have this test.html:
<h1>Hey World</h1>
The problem is it does not load the Hey World into the main page. Also, there is no error in console.
What is the problem?
As with $('#result'), jQuery is unable to find the element as DOM is not ready hence jQueryObject.load method is never invoked.
Wrap your script in $(document).ready or Place your <script> as last-child of <body>(Just before closing body tag(</body>))
There are also DOMContentLoad or window.onload events which makes sure that DOM is loaded but IMO, placing <script> just before </body> is easier option.
<div id="result"></div>
<script>
$('#result').load('test.html');
</script>
You are trying to append to an element that yet does not exists.
Try adding your code in a $(document).ready(), and just before the </body> closing tag.
Like this:
<script>
$(document).ready(function(){
$('#result').load('Ajax/test.html');
});
</script>
</body>
Try changing your <script> tag to include a document.ready:
<script type="text/javascript">
$(document).ready(function () {
$('#result').load('Ajax/test.html');
});
</script>

jQuery Not Working in Eclipse

Still a bit new with jQuery so I may be making a basic error.
I just completed the jQuery introductory course and now am trying to do some of my own basic work, but have hit a slight road block.
Essentially it seems the jQuery file script.js isn't getting called properly.
When I ran it inserting it into stackoverflow it seems to work fine. However,
when I pull the html file in the browser it only displays the html elements in the file and not any of the jQuery code.
Any help would be appreciated!
$(document).ready(function() {
$("body").append("<p>I\'m a paragraph!</p>");
/* write 'Hello World! to the first div' */
$("#first").append('<h1> Hello World!</h1>');
//a clickable 'Hello World!' example
$("#link").click(function() {
$('#greeting').append("<h1>Hello Again!</h1>");
});
});
<!DOCTYPE html>
<html>
<head>
<title>Hello World - jQuery Style</title>
<script type="text/javascript" src="script.js"></script>
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="stylesheet.css"/>
</head>
<body>
<div id="first"></div>
<div id="second">
Click Me! <br /> <span id="greeting"></span>
</div>
</body>
</html>
You should declare your current javascript file after you get the jquery.min.js
If you look in your console ( right click and inspect ) You will notice that it cannot understand $ jquery sign.
Solution should be just swap these two lines in your html :
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="script.js"></script>
Try switching the order of the tags for script.js and jquery.min.js so that jquery is initialised first

jQuery mobile - DOM Exception 18

I am trying to experiment with jQuery mobile, but can't seem to get started.
I have the following HMTL file hosted on a local server:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/mobile/1.0/jquery.mobile-1.0.min.js"></script>
</head>
<body>
</body>
</html>
Which is causing
Javascript error: undefined SECUIRTY_ERR: DOM Exception 18
when accessed from my iPhone from http://192.168.1.1:8000/mobile.html
Did you link the CSS stylesheet file as well before the jquery script include tags?
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.1.1/jquery.mobile-1.1.1.min.css" />
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.1.1/jquery.mobile-1.1.1.min.js"></script>
Also, in order for the page to show, you must contain content into containers like this:
<body>
<div data-role='page'>
<div data-role='header'>
Header goes here
</div>
<div data-role='content'>
Content goes here
</div>
</div>
</body>

Categories