Getting data attributes from script code using Javascript [duplicate] - javascript

This question already has answers here:
How may I reference the script tag that loaded the currently-executing script?
(14 answers)
Closed last year.
How can I console.log the data-text value in that javascript file? Noob here still learning. Thanks for the help or information..
Sample:
<script type="text/javascript" src="./test.js" data-text="Hello World"></script>

You could use document.querySelector() combined with the Attribute Selector to select the script like so
document.querySelector('script[data-text]').dataset.text

Related

Am I able to put my <script> tag outside of the <html> tag? [duplicate]

This question already has answers here:
Is it wrong to place the <script> tag after the </body> tag? [duplicate]
(10 answers)
Loading script outside of html document
(3 answers)
Closed 3 months ago.
<html>
<!--A bunch of code-->
</html>
<script>
//A bunch of code
</script>
<style>
/*A bunch of code*/
</style>
Does this work? I ran this with VS Code and it seemed to work, but I'm yet to find any other code that runs like this. I use it so that I can easily shrink the HTML to work on the JS. Would this formatting give me any problems at any point?
I tried this style with VS Code and it seemed to work fine.

How to add link to a button in html without using javascript? [duplicate]

This question already has answers here:
How do I create an HTML button that acts like a link?
(35 answers)
Closed 3 months ago.
button tag without using java in html
I have try using javascript but i'm curious to know is their something we can use it without javascript
You mean like this?
<button>Link Button</button>
but why?

Why would a <script> tag contains HTML comments in it? [duplicate]

This question already has answers here:
Are HTML comments inside script tags a best practice? [closed]
(10 answers)
Closed 2 years ago.
In this app I'm working on I see this pattern a lot:
<script type="text/javascript">
<!--
doStuff();
//-->
</script>
What purpose might those comment tags serve? Can I safely remove them?
In ancient times some browsers didn’t understand the <script> tag so it was made in a way that you can add comments to the beginning without issues. This causes anything inside to not be shown on the page if the browser doesn’t know what to do with the tag.
This hasn’t been needed in a very long time so it’s safe to remove them.

Can a block enable loading of css/js? [duplicate]

This question already has an answer here:
How to attach js/css to a StreamField block when it is rendered?
(1 answer)
Closed 4 years ago.
I want to create a block that is going to utlize some js/css from a CDN, and possibly some custom js code just before the </body>. I do not want these loaded unless the block is used on the current page. Is there a way I can check if the block has been used, or even better, have the block tell the base.html file that it is being used?
If you already have jQuery in your project, the easiest way to do this will be with getScript. Documentation is here: https://api.jquery.com/jquery.getscript/
With plain JS, you have to jump through slightly more hoops and create a <script> tag that you then append to the document:
var dynamically_loaded_js = document.createElement('script');
dynamically_loaded_js.setAttribute('src','http://example.com/dynamically_loaded.js');
document.body.appendChild(dynamically_loaded_js);

Does order of attributes matter in script tags? [duplicate]

This question already has answers here:
Does the order of HTML attributes have any effect on performance?
(2 answers)
valid order for attributes of input type tag
(5 answers)
Closed 8 years ago.
I have a WordPress plugin that enqueues a Javascript file. This is the current format:
<script src='http://www.mywebsite.com/myscript.js' data-cfasync='false'></script>
The 'data-cfasync' attribute is to instruct CloudFlare Rocket Loader to ignore the script, per their KB.
However, CloudFlare's URL format is like this:
<script data-cfasync="false" src="/javascript.js"></script>
Notice the data-cfasync attribute is before the src attribute. Does that make a difference? Does the order of attributes matter?
The order of attributes in HTML elements doesn't matter. You can write the attributes in any order you like id last onclick first - it does not matter.

Categories

Resources