I am importing an html document, form.html, from a script located in my index.html file.
form.html is a basically a fragment of HTML and at the bottom of the html is an internal script that attaches the html to the main document:
Afterwards, I reference an external script that is supposed to do some stuff to the HTML in form.html. However, this fails to have an affect in Firefox.
If I inspect the <head> of the main document in Firefox developer tools, I can see the "document fragment" composed and with the correct scripting affect. However, the actual imported HTML that appears in the body is unaffected.
I have tried inlining the script at the bottom of form.html. I also tried using window.onload to attach the external script to the end of the body of the main document as well as trying to use a link tag as per this question.
Not quite sure what else to try. I would like to keep the script modular and contained inside form.html if possible as to only request it when that HTML page is requested.
Thanks,
When using the HTML Imports polyfill, the processing of the imported document is only asynchronous. So you should wait for the HTMLImportsLoaded event or use the HTMLImports.whenReady() method to be sure the content was imported.
<head>
<script src="html-imports.min.js"></script>
<link rel="import" src="from.html">
<body>
//code to be injected
<script>
document.addEventListener( 'HTMLImportsLoaded' , ev => {
//you can safely access the imported code in the body
} )
</script>
Related
I have a java script function test1.js. The function loads external scripts on demand and injects it in the head. As part of the vendor documentation we need to load few scripts in the html head and One of the external script is loaded as follows
<script>esmDefine(["https://website1.com/web1-component.js"]);</script>
I want to do the same in java script and inject the above line in the html tag.
I tried the following
let scriptElement = document.createElement('script') scriptElement.textContent = 'esmDefine(["https://website1.com/web1-component.js"])' document.getElementsByTagName('head')[0].appendChild(scriptElement);
Tried
scriptElement.innerText, scriptElement.innerHtml etc but nothing worked as expected.
Result expected will be
<html>
<head>
<script>
esmDefine([
"https://website1.com/web1-component.js",
]);
</script>
...
</head>
Any ideas in this regard is appreciated
I guess you're facing this issue because you're trying to append a script tag in your head tag, that has a function call within it, so as soon as your script tag is injected into the head of your page, the page tries to execute the esmDefine function. But there must be some other external script file that has it defined, as you're saying that you've not defined it anywhere on your own. Then in order for this function to execute, the base declaration for this needs to present there, I mean the page's JavaScript renderer should know how to execute this function and the file that has this functions definition, is an external script file that you linked to your page, but how JavaScript rendering works is, it first parses the page (HTML, CSS, JavaScript (everything that is there on the page)), that you've added as inline to the page, not the external files. Then when everything is done loading then it goes on to fetch the external references and add those later to the CSS Object Model & JavaScript Object Model. But since you're adding this function call as internal to your page, it'll immediately try to execute it and will fail since the definition to this function (the external script file) is not yet loaded. You can try appending this script tag (with your esmDefine) to your page's head inside the window's load event listener. This will be fired only when everything on the page, including the dependent external resources are done loading on to the page. This way you'll not encounter this not defined ReferenceError.
I hope this will help you to move forward.
If you want to import a js script in a script tag I think you could import it or use a src="" if you do not need to interact with the script and know how it behaves.
Maybe try something like:
<html>
<head>
<script>
import esmDefine from 'https://website1.com/web1-component.js'
// esmDefine() ?
</script>
...
or :
<html>
<head>
<script>
import { esmDefine } from 'https://website1.com/web1-component.js'
// esmDefine() ?
</script>
...
or :
<html>
<head>
<script src='https://website1.com/web1-component.js'/>
...
I am given this code which should display an embedded small coupon version of this page https://weedmaps.com/deals#/1118217:
<script type="text/javascript">var coupon_id = 17811;</script>
<script type="text/javascript">var coupon_type = "deliveries";</script>
<script type="text/javascript" src="https://weedmaps.com/embed/coupon.js"></script>
I do not know how to add the JavaScript to the HTML correctly. I have placed the following scripts in the head section. But I don't understand how to have the coupon generate in the div I want it to. I have tried calling the JavaScript function, but I've never worked with JavaScript before. Could someone please help me embed this coupon.
I've taken a look at your script and first of all: it definitely should be placed inside the document and not into the <head> section because coupon.js is writing out html at the placement of the coupon.js script import.
In theory you just need to place the script and it should work but there are some problems:
You need to execute it on a web server - when running as a plain html file the script just tries to find the libraries in your file system which does not work.
It still would not work because it would still try to find some resources at your web-server. In mycase it the script tried to load http://localhost:63342/restpoints/deliveries/17811/deal which will not work
To prove 2. just try https://weedmaps.com/restpoints/deliveries/17811/deal with the correct domain. Then you are receiving correct JSON which is used to fill the coupon pane.
=> Consequently the script you were given has problems if it should be executable from domains different from "weedmaps.com"
Javascript can be between head tag but it advisable to put it below before the body closing tag, to allow your page contents loads first before loading javascript. Just import your javascript. and call out. Hope this was helpful.
<!DOCTYPE html>
<html>
<head>
</head>
var coupon_id = 17811;
The JS indicates it is looking for an element with an id of #weedCouponPane. Do you have this in your html? i.e.
<div id="weedCouponPane"></div>
I want to have an external .html file, from which I will load snippets programatically, and insert them in the document at runtime. I did it like this in my index.html
<head>
<link rel="import" href="html/html_snippets.html">
</head>
This is the example content of the html_snippets.html file
<div id="asd">
<some stuff here>
</div>
Then after everything is loaded, I load the snippet like this
var friendSnippet = $("#asd").html();
The problem I am facing now is that I use the jQuery code in a loop, which runs several times, and the first time it always returns an undefined, but after that it's fine.
Why is that happening?
You can use the jQuery .load() function to load an html file into a certain element on the page.
$("#destinationElement").load("path/to/file.html");
This may solve the issue.
I have SVG files that I need to embed inline in an HTML file (using the HTML5 <svg> tag). Those SVG files have a JavaScript dependency, which looks like this:
<script type="text/javascript"
xlink:href="http://gnuplot.sourceforge.net/demo_svg_4.6/gnuplot_svg.js">
</script>
Here is a standalone SVG image with that script dependency. To activate the JavaScript, you can click on the image, and then there's a box that follows your cursor telling you your (x,y) coordinates.
However, here is the same image embedded directly into an HTML document. According to the web inspector, the browser never even tries to load the JavaScript dependency. When you click and move your cursor around the image, the console fills up with a bunch of reference errors.
How do you make the embedded version load its JavaScript and function like the standalone version?
The code in the document you link uses a self closing <script> and that does not work (unsure if this is a bug in browsers or not).
The code as you write it:
<script type="text/javascript"
xlink:href="http://gnuplot.sourceforge.net/demo_svg_4.6/gnuplot_svg.js">
</script>
will at least load the script, but then the script assumes the document's root element is an <svg> element (it uses getScreenCTM) and in an HTML document the root element will be an <html> element.
Maybe it won't help you, but you need a different version of the script.
I am little new to web development and when I was searching internet about other topics, I have seen many people has put popular JS Libraries in Different Places of their websites.
eg: Inserting JS Libraries on the Very Beginning or Start of the <head> </head> section. (Before loading any JS Code or a CSS File)
eg: Inserting JS Libraries on the End of the <head> </head> section. (After loading all JS Codes and CSS Files)
eg: Inserting JS Libraries on the End of the <body> </body> section. (After loading all JS Codes, Texts, Images, Videos, CSS Files etc...)
So my question is this.
What is the best practice for inserting (where) following JS Libraries, Plugins and CSS Style Sheets to a web page for the most faster loading times and other advantages? - Please mention the reason -
JQuery and it's Plugins
Bootstrap 3.js
Modernizr.js
Angular.js
And another widely used JS Libraries which I couldn't mention here...
Normalize.css
reset.css
bootstrap.css + more
Thank You..!
There is no so called "standard" method. The choice of where to put the lines boils down to one question: When will I need the library?
You see, web files loads line by line, let's take the following as an example of what I mean:
<script>
document.getElementById("target").innerHTML = "changed"
</script>
<p id="target">unchanged</p>
#target isn't altered because the script was loaded before the element did. Web files loads procedurally. When the line of JavaScript is loaded. It is executed immediately, but the target element isn't. So it couldn't change the element.
Even with jQuery, there is the same problem:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$("#target").text("changed");
</script>
<p id="target">unchanged</p>
Therefore, we often use $(function(){}).
Back to the loading problem
People who put their <script> tags or <link> tags in the body (in front) or in the head, wanted to execute the script immediately, sometimes they won't use $(function()) or document.onload
People who put their <script> tags or <link> tags in the body (in the end) wanted to ensure all elements are loaded then execute the script or load CSS.
Conclusion
You should load independent resources such as jQuery first, then load dependent resources such as jQuery plugins. Then you try to decide when you want the resources to start loading, then put the lines in places.
You should put CSS links in the <head> tag because you don't want visitors seeing unstyled content before loading the CSS files.
If you can't decide or don't care about the time, put every <script> and <style> tags in the <head>.
Here is another post you might be interested in: Load and execution sequence of a web page?
CSS can added inside header tag & but put all JS Libraries and custom files just before closing closing body tag
<body>
//other tags
<script> All Scripts here </script>
</body>
By doing so you wont have to check if DOM content has loaded.
It decrease page loading time.Otherwise a js need to be completely loaded before DOM loading.
It also makes sure that all events are attached properly to DOM element.
I think this address all your concern specially the third one
CSS Sheets go in the < head >. The order of the CSS files matter so libraries should be put in first then you can put in the specific ones you have.
Javascript links go in the < body > but place them at the very end. That way your HTML content loads first then the JS loads and it will recognize all your selections. It is more efficient to do it this way.
The most important thing to note when placing your css and script tags is that the order you place them determines the order they are loaded in and if style or code is loaded later it over writes the code written before. So if you have css styling that assigns different styles to the same attributes of the same element then it is the one loaded later that takes effect. And with script tags it's important to remember that for dependency reasons. You should load the dependencies first so that they are there for the other scripts to use. Aside from that normally css tags are in the head and script tags at the bottom of your body element