Including JavaScript - javascript

I know it is possible to put JavaScript is a specific .js file and then include this in any page by just doing...
<script src="MyJavaScript.js"></script>
I note that with respect to these .js files that are included:
They do not actually have to end with .js
Their content should not be enclosed with tags
However, I need to import some JavaScript that is in a file ending with extension .page (I am using force.com platform) where th JavaScript is enclosed with the tags. The reason I need to do this is so I can write som unit tests using quint. Now, my hands are tied here. The cleanest way is to obviously refactor out the JavaScript from .page file into a .js file but I can't do this. I want to know is there any way I can just import the script snippets enclosed with tags.
Many thanks

As #totero said, you can specify the script type with the attribute
<script type="text/javascript" src="myFile.annoyingExtension">
But please, don't forget to close the tag otherwhise you will get strage bugs:
<script type="text/javascript" src="myFile.annoyingExtension"></script>
Close the tag itself wont work so please, don't do this
<script type="text/javascript" src="myFile.annoyingExtension" />

Is this what you are looking for?
<script type="text/javascript" src="MyJavaScript.page">
John Resig has a good approach to deprecate Tags. Might be worth a look.
There is also a SO thread on this Tag issue. Please see here.

You mention including Javascript, and by the looks of it, you're including them in a HTML document. Do you have any other programming or scripting language available to you that allows you to edit the document? If you can use something like PHP or ASP within the platform, you could have it read the .page file and echo it into your HTML output document (possibly stripping the <script> tags from it at your convenience).

Related

Why my script.js file doesn't work but the JavaScript code inside of the index.html file works?

When i add this:
<script src="script.js"></script>
<script src="jquery-3.5.1.min.js"></script>
to my index.html file in the head of the file, and i create a JavaScript file, it looks like the html doesn't recognize it.
But when i add this:
<script type="text/javascript">
//code
</script>
to my index.html file in the body of the file, all of the code i done in JavaScript works.
Can somebody explain why this happens?
It’s always a good practice to add jQuery code just before the closing </body> tag. If you have not done that, then use the defer attribute.
Also if you’re writing jQuery code make sure that your code is placed after the jQuery link code.
Don't add your JS files in the head. You are most likely going to be accessing DOM elements, and those elements are defined in the HTML. If you insert a script above the html, the elements won't be recognized because they're not defined yet. Add all of your scripts after the body, I add mine after the closing html just to be safe. And please add your script after your jQuery script.

sigma.js How to plug a plugin on my own page

I'm totally new at coding and i have some trouble using the dragNodes plugin. I can make the plugin work by itself with the generated random graph example, but I am struggling to make it work with my own graph.
I don't understand what to put in the html. I tried to put only that:
`<script src="sigma.js/plugins/sigma.plugins.dragNodes/sigma.plugins.dragNodes.js"></script>sigma.plugins.dragNodes(s, s.renderers[0]);`
and it doesn't make my nodes move. I think I have to change the content of "s" and "s.renderers[0]" but I cannot find where., and I don't know how to do it...
Basically, I would love if anyone could give me some explanations about how to plug a plugin in my page?
If you could help me, I'm lost and that would be awesome! Thank you a lot!
I think you are mixing up two things:
How to use script into html
How to include external js files in a web page.
In order to insert javascript directly into an html page you have to use the script tag as follow (called inline js):
<body>
<script>
// some inline js
var a = 1;
</script>
</body>
In order to include js from an external file you have to reference the file with the src attribute just like you did.
<script src="sigma.js/plugins/sigma.plugins.dragNodes/sigma.plugins.dragNodes.js"></script>
The only thing that you missed is that sigma.plugins.dragNodes(s, s.renderers[0]); is also some js code, you thus have to put it into script tags to that it can be interpreted as such by the browser.
Here is what you probably are trying to write:
<script src="sigma.js/plugins/sigma.plugins.dragNodes/sigma.plugins.dragNodes.js">
</script>
<script>
sigma.plugins.dragNodes(s, s.renderers[0]);
</script>
Having all this into <body>tags.
I should also mention that to render your graph using sigma you should have an instance of sigma in your page, and in your case it should be called s.
To learn more about js/web programming I would advise this: http://www.codecademy.com/
As far as documentation for sigma is concerned check out their tutorial: http://sigmajs.org/
In addition to Nicolas Joseph's answer (put javascript in script tag), you should also download the js library that is required and save the html file in the appropriate path.
For example if your file.html is in a directory dir (dir/file/.html)
Then the command:
<script src="sigma.js/plugins/sigma.plugins.dragNodes/sigma.plugins.dragNodes.js">
</script>
will search for a file named sigma.plugins.dragNodes.js in the following path:
dir/sigma.js/plugins/sigma.plugins.dragNodes/sigma.plugins.dragNodes.js
To check out which librarires you are missing you should open debug mode in your browser (right click->inspect element) and check the console tab.
Cheers

How to hack LiveScript in order to link a separate file in html

We could include separate coffee-script file in html with:
<script type="text/coffeescript" src="/static/webpage.coffee"></script>
But if we want to use LiveScript in browser, they say:
If you use this, your LiveScript scripts must be inline (not linked to with the src attribute), be placed after the included livescript.js file, and the script tags must have the attribute type="text/ls".
I need exact same include technique we used to use for coffee-script or Javascript. So, is there anyone give me a tip to start hacking?
Looking at LiveScript.go() in src/browser.ls (browser/livescript.js) it is apparent that external files are actually supported. Indeed, the following worked for me:
<head>
<script src="livescript-min.js"></script>
<script type="text/ls" src="script.ls"></script>
<script>
require('LiveScript').go();
</script>
</head>

Do JavaScript Style Comments Have to Go Between Script Tags?

I am new to JavaScript, and could use some clarification on the appropriate way to place JavaScript comments within an html file. I have spent time looking this up on the web, but I can only find instructions on how to comment in JavaScript not where to comment. I understand that comments within the script tags are fine (so long as they are not excessive). My question is whether or not it is ok to put JavaScript comments just before the script element. I want to know whether it is syntactically ok, and whether it is acceptable professional practice.
Example:
// Added link to external js for TUT 1 Case 3
<script type="text/javascript" src="../Data Files for Text/Tutorial.01/case3/functions.js"></script>
Is that acceptable, or should I be using html comments outside of the script since this is within an html file? Thanks for any help.
If you're using a markup language (e.g., HTML), use markup comments.
If you're in a <script> tag, use script comments.
If you're in a <style> tag, use style comments.
Examples:
<!--
HTML comment
(multi-line)
-->
<script type="text/javascript">
// script comment
/*
script comment
(multi-line)
*/
</script>
<style type="text/css">
/*
style comment
(multi-line)
*/
</style>

Web address virtual directory breaks external javascript link so it cannot find the files

so i have a little problem
my webadress looks a little like this:
http://localhost:23425/dynamicName/Home.aspx
the dynamicName part well....will be dynamically named. So because of that it breaks my
<script type="text/javascript" src="Scripts/jquery.js"> </script>
scripts since it can't find it. however if its
http://localhost:23425/Home.aspx
then the javascript works.
So my question is how can i get the javascript to work on the dynamic virtual directory and not have my links be broken?
i've tried this :
<script type="text/javascript" src="<%=ResolveClientUrl("~/Scripts/Menu.js")%>"> </script>
with the script links and the css links but it doesn't seem to do it. just a grey box drops down with no content.
Any help will be extremely apprectiated
Try:
<%=ResolveUrl("~/Scripts/Menu.js") %>
ResolveClientUrl creates a path which includes ../. That may be your problem.

Categories

Resources