How to include js file from shared location in sharepoint master page - javascript

I would like to include js file from different location that it's now.
Currently in master page code it looks as below:
<SharePoint:ScriptLink ID="ScriptLink1" language="javascript" name="Company/Scripts/myJsfile.js" Localizable="false" runat="server"/>
and it works fine.
But what I'd like to do is to change current location of this javascript file. Target location of this .js file is a shared location (" \\v-share\share\javascript ").
Is there any way to include this .js file from shared location in sharepoint master page ?

You could try this :
<script type="text/javascript" src="\\serverip\path\to\jsfile" ></script>

Related

My js array wont display with console.log in chrome browser

console.log doesn't display what I want it to show in my chrome browser dev console.
I have this code:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<script src="script.js">
console.log(amazingCars);
var amazingCars =["BMW","Lexus","Ford","Mercedes"];
amazingCars.pop();
console.log(amazingCars);
amazingCars.sort();
console.log(amazingCars);
amazingCars.length;
console.log(amazingCars.length);
</script>
</body>
</html>
The directions for the assignment are the following:
Link the JavaScript page to the HTML page.
Create an array named amazingCars that contains four values:
BMW
Lexus
Ford
Mercedes
Remove the last value from the array
Next, sort the array in alphabetical order
Lastly, find the length of the array
I dont understand why it isn't displaying or the reason of this issue
If you are putting your JS code in html just add them between<script></script> tag.
You do not need to add src for script unless your JavaScript code is coming from a separate script file.
The src attribute specifies the URL of an external script file.
If you want to run the same JavaScript on several pages in a web site,
you should create an external JavaScript file, instead of writing the
same script over and over again. Save the script file with a .js
extension, and then refer to it using the src attribute in the
<script> tag
.
As per the instructions that you are following : "Link the JavaScript page to the HTML page."
Create another file "index.js" in your project directory.
Move your js code into the index.js file in your project directory and then use :
<script src="index.js"></script> in your HTML.
Firstly, make the <script src="index.js"></script> seperate from the code in the head, also unless if you have a seperate javascript file you dont need the src part at all, and secondly the first console.log outputs it before definition, creating an error. I hope this helped
You have to change <script src="script.js"> to <script>.
Alternatively you can keep it this way and create a script.js file and copy paste all the code you have inside the <script> tag to the file. This file need to be in the same folder as your index.html file.

Why do some .js files stop working when downloaded to localhost?

I have a problem that when I use a code that links to a .js file available online, the code works fine but if I try to save this .js file on localhost and then link to it, the code doesn't work. For example, This code
<!DOCTYPE html>
<html>
<head>
<title>MathJax TeX Test Page</title>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']]}});
</script>
<script type="text/javascript"
src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
</head>
<body>
When $a \ne 0$, there are two solutions to \(ax^2 + bx + c = 0\) and they are
$$x = {-b \pm \sqrt{b^2-4ac} \over 2a}.$$
</body>
</html>
works fine, but when I downloaded http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML to localhost and then link to it by changing
<script type="text/javascript"
src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
to
<script type="text/javascript" src="js/MathJax.js"></script>
This code stopped working. I encountered the same problem with pdf.js. This problem doesn't happen with every .js file. For example I downloaded jquery and jquery-ui files to localhost and they worked fine. Also, when I link to my own script files that I wrote the code works fine too.
I have checked that MathJax.js is in the js folder and its with other js files like jquery.js and usually when I use jquery.js I just use the line
<script type="text/javascript" src="js/jquery.js"></script>
My question is: Am I doing something wrong or do these files only work online?
Debug :
Open chrome developer tools by right clicking on page and doing
inspect element or view > developer > developer tools.
Navigate to network.You might have to refresh page after going to network.
Do you see file loads successfully?
If it's red then that means it didn't load correctly.
Right click and open it in new tab and verify path.
Update :
MathJax is a package so you have to link entire package rather than just individual file. Refer here http://docs.mathjax.org/en/latest/start.html#downloading-and-installing-mathjax
Make sure the link to the javascript file is correct.
<script type="text/javascript"
src="js/MathJax.js">
</script>
The above script says the file MathJax.js is inside js directory AND the js directory is at the same level as the file you are linking from
<script type="text/javascript"
src="./js/MathJax.js">
</script>
The ./ gives the file a relative startpoint. However, if the javascript is not in a folder entitled "js" which is sitting in the same folder as your html, this won't work.
The other solution is to build the entire absolute file path to the file in your tag, and end with "/MathJax.js".

dynamic reference to javascript file

I have a master page with a reference to javascript file that is js folder.
Both File and masterpage are in the root directory.
Many of my forms are in different folders Like
Root>first>abc.aspx
Root>second>def.aspx
Root>second>anotherFolder>def.aspx
I kept the reference like this
<script type="text/javascript" src="js/Check.js"></script>
I also tried src="<%= Page.ResolveUrl("~/js/Check.js") %>" and src="~/js/Check.js". While using Page.ResolveUrl and Server.ResolveUrl page shows error.
What i need to do so that any form in any directory get the reference of the file.
I dont want to do ../ and ../../ in every form.
<script type="text/javascript" src="/js/Check.js"></script>
you can use "/" --> web site root
If you're running your Visual Studio's internal server for debugging then firstly remove the virtual folder it insists on using.
Do this by selecting the root of your site in VS Solution explorer, then right-click and choose "Properties Window" - in the properties change "Virtual Path" from your "AppName" to /
This virtual path plays havoc with all sorts of paths..I find that if i don't make this change in VS then when I DO get all my paths working they won't work when I put the site on a live server that isn't using a virtual site.
Then set your JS reference to <script type="text/javascript" src="/js/Check.js"></script> - using the root / in your src.
UPDATE
If you really want to keep the virtual folder then you can try this - using ResolveClientUrl():
First, add Runat="Server" to your <head>
<head runat="server" id="myhead">
<script type="text/javascript" src='<%=ResolveClientUrl("~/js/check.js") %>' />

Where do I store files for the src method?

I am learning JavaScript and there is this:
<script type="text/javascript" src="myjava.js">
</script>
Where would I store "myjava.js"? Would it find it if the file is on my desktop?
You should put the javascript file in the same folder your html file is residing. I highly discourage the use of absolute path to specify the src since you will have to edit the references once you move your file from one place to another. Also, a good organization is necessary while writing web apps so I recomment you to create a proper directory structure such as:
webroot
yourpage.html
js
myjava.js
css
styles.css
images
1.jpg
With such directory organization, your work looks properly structured. So now you can reference the javascript file as <script type="text/javascript" src="./js/myjava.js"> </script>
For static html pages, the JavaScript file must be in the same folder as the html file, from which it is called. Otherwise you should use a relative path - ../path/somefolder/myjava.js or full path.
Example:
<script type="text/javascript" src="C:/Documents%20and%20Settings/USER/Desktop/some.js"></script>
The value value of the src attribute of a <script> element is the URL to a script file. This URL can be relative or absolute.
If the page containing the <script> element is located at, for example, http://mysite.net/someDir/myPage.htm, and the script file is in the same directory, you can use any of the following for your src attribute.
<script src="myscript.js"></script>
<script src="/someDir/myscript.js"></script>
<script src="//mysite.net/someDir/myscript.js"></script>
<script src="http://mysite.net/someDir/myscript.js"></script>
If you are loading the page from your computer, save the script file in the same directory as your html file, and use a relative path.

How to put JavaScript from tutorial into my website?

I'm new to JavaScript and I'm trying to get a slideshow working in my website. I'm following this tutorial: http://www.queness.com/post/1450/jquery-photo-slide-show-with-slick-caption-tutorial-revisited
Unfortunately it doesn't go into a beginners level of detail as to how to get it installed. I know what I should do with the HTML and the CSS but I'm not sure where I should be putting the JavaScript. Does it go in a separate file or something? How do I get it to work?
Also I'm trying to do this within cakephp so if there's any specific cakey thing I can do that'd be awesome!
Copy all that JavaScript on your referenced page. Save to a new file slide.js or something like that.
Edit your HTML document to include a reference to the jQuery libraby, and that new JavaScript file you've just created:
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
<script type="text/javascript" src="http://mysite.com/slide.js" />
You can put JavaScript in an external file with a .js extension, and include that in an HTML page using the script tag:
<script type="text/javascript" src="yourscript.js"></script>
Alternatively, you can write the script directly in the script element:
<script type="text/javascript">
//Your script here
</script>
Also note that as the tutorial you are following uses jQuery, you will need to download and include the jQuery library in your page.
The script tags can be placed anywhere in the document, but are usually found inside the head tag, or just before the closing body tag.
Welcome on SO Helen.
That script uses the jQuery library so you have to also include that. http://jquery.com/
JavaScript can best be placed in seperate file with the extension .js
Add the javascript files just before the </body> tag on your page like:
<script src="/js/jquery/jquery-1.4.4.min.js" type="text/javascript"></script>
<script src="/js/jquery/jquery-slideshow2.js" type="text/javascript"></script>

Categories

Resources