This question already has answers here:
How to load local script files as fallback in cases where CDN are blocked/unavailable? [duplicate]
(10 answers)
Best way to use Google's hosted jQuery, but fall back to my hosted library on Google fail
(23 answers)
Why do I link my jquery inside a document.write?
(3 answers)
Closed 5 years ago.
I'm trying to learn Bootstrap and I was studying this example. I noticed that they include the Jquery library and other scripts at the bottom of the markup just before the end of the body-- this part I understand. What I don't get is why JQuery is included using two lines of markup, like this:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="../../assets/js/vendor/jquery.min.js"><\/script>')</script>
What is the purpose of the second line?
Is it meant to handle the case where the first line fails or is too slow? Does writing the script element using document.write guarantee an immediate load time? Or what is the purpose?
Related
This question already has answers here:
Limit scope of external css to only a specific element?
(6 answers)
Closed 2 years ago.
when using jQuery load() to inject an HTML page into a DIV, the HTML could bring CSS files in its head, importing them in the DOM.
The problem is that these injected CSS rules are then applied globally, which is not always the case I need.
Is there a way to avoid this behavior and limit the imported HTML pages CSS only within the hosting DIV?
Just get the content:
$("#myDiv").load("page.html #contentContainer")
or
$("#myDiv").load("page.html body")
This question already has answers here:
How to add meta tag in JavaScript
(5 answers)
Closed 3 years ago.
I want to add meta tag conditionally in an HTML page, i am not sure if it is ok to do so or not using jQeruy ready even. The example is given bellow.
var myHost = window.location.host;
if(myHost.startsWith("techstag")) {
$("head").append('<meta name="robots" content="noindex,nofollow">');
}
In short, it may or may not work.
It would work in the sense you'll see the meta tag in page when you inspect it.
But, if you are intending to do this for SEO purposes
For the most part, it won't work, as most web crawlers do not execute javascript while scanning the web pages.
It will work for crawlers that do execute JS (e.g. Google's)
This question already has an answer here:
Conflict Bootstrap, Prototype Js and Jquery
(1 answer)
Closed 6 years ago.
I am working on a site that uses javascript for a dropdown search list. The site also has a popover. When I put the source below into the header the dropdown search list does not have a search function working, however the popover works, and vis-versa.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>-->
Popover
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
Is there a way to make the scripts relate to specific source?
Thanks in advance.
I figured out the issue; the src had to be higher up in the Header. This page helped me: Can I use multiple versions of jQuery on the same page?
This question already has answers here:
Can an iframe only show a certain part of the page?
(8 answers)
Closed 8 years ago.
I am trying to build a webpage using wix.com, and I want to include part of another website into mine. I can add html code. I am trying to get the featured poet on poetryoutloud.org into mine. This changed everyday and I only want this part of the webpage. I know that if I wanted to include the whole webpage I could use an iframe element, but this is only part of it
You can use an iframe
or alternatively you can use ajax, here an example using jQuery.
$("#yoursite-html").load("yoursite-html.php");
Please note if content is on another domain as described in your answer (CORS issue), you have to use a simple proxy example below:
PHP in file "yoursite-html.php":
echo file_get_contents("http://www.yoursite.com/");
This question already has answers here:
Unobtrusive JavaScript: <script> at the top or the bottom of the HTML code?
(7 answers)
Closed 8 years ago.
I have listen a lot that we should always load the java script at the end of the page why we should do that. If i write the java script at starting of the page how it will make the difference?
If you write javascript at the start of the page, than you'll not be able to access the DOM elements, directly.
However when you use it at bottom, all the elements will have been rendered and you can use them.
In the first case, you would need something like this:
window.onload = function(){
document.getElementById('id');
}
But in the second case, you need just:
document.getElementById('id');
Also, if you have scripts at the start of the page, it will block the UI from rendering.
If you're using JS to manipulate the DOM, you'll want the page to be loaded before the script is run - usually this means placing the script after the page content.
However, if the Javascript is in response to say an onClick event, you shouldn't need to put it at the base of the page.