launching an external javascript file syntax - javascript

I was wondering about the syntax of running external .js files, when you do it normally you just do
<script src=http://www.example.com/javascript.js></script>
but when running code in chrome (through the url bar), where the syntax is:
javascript:javascriptcodegoeshere
The <script> tags aren't included, so how can you launch a js file through using that?

You have four options.
You can put the actual code in the <script> block.
<script>
javascriptcodegoeshere
</script>
You can save the code in a file and refer to it.
<script src="http://www.example.com/javascript.js"></script>
You can put it as a data URL.
<script src="data:text/javascript,javascriptcodegoesgere"></script>
You can put it in a link if you want the user to launch the code.
link

Related

Can't load javascript file in localhost using xampp

I am new at web development and I want to upload a main.js scrip in my PHP file but it gives me this error
I used a script tag to upload the js file like this:-
<script type="text/javascript" src="../assets/js/main.js"></script>
This could be due to many things:
Check ad/script blocker
Check if the browser can load it from the view page source option
Try pasting it before all scripts call it could be due to some functions in previous scripts, I faced this issue before.
This might help.

How to properly call internal js.file after building in Javascript

I have a problem with initializing internal file.
Specifically https://www.detectadblock.com/
I can't initialize this ads.js file on my project.
var e=document.createElement('div');
e.id='punVTqCWOHsB';
e.style.display='none';
document.body.appendChild(e);
My web browser always failed with loaded script. Any suggestions how can I proceed?
Loading failed for the with source “127.0.0.1:8888/AdBlock/src/com/example/myproject/public/ads‌​.js”." github.com/marosmamrak/AdBlock-1.git
You are giving the full path to the source files (in src/.../public), but that file will be copied with the rest of the GWT compiled sources, so it should be inside the adblock directory to match your .nocache.js file:
<script type="text/javascript" language="javascript" src="adblock/adblock.nocache.js"></script>
<script type="text/javascript" language="javascript" src="adblock/ads.js"></script>
Also please note that this does nothing at all, since the browser can't execute Java files directly:
<script src="/AdBlock/src/com/example/myproject/client/Flipper.java"></script>

Javascript doesn't work if I invoke it as URL

I want to run a Javascript file in my page. If I embed it as a script inside of the HTML, it works. I mean, the following code works fine:
<html>
<script>
...
</script>
</html>
But when I reference an URL source (with the src attribute), it doesn't work:
<html>
<script type="text/javascript"
src="https://s3-sa-east-1.amazonaws.com/XXXXX/myJavascript.js">
</script>
</html>
When I put the URL in my browser, the code is displayed.
EDIT: Do I need some especial configuration in the Amazon S3 Servers in order to work with my Javascript files??? I just uploaded it and marked it as public resources, but I did nothing more.

Can't access Javascript file

I'm using webform asp.net and C#. I linked the master page with javascript file like this
<script type="text/javascript" src="javascript/main.js"></script>
but when I'm going to inner pages inside folders.. I can't access the JavaScript file.. (404 error).I tried to solve the problem by using ResolveClientUrl but it's not work!
What's the problem?
but when I'm going to inner pages inside folders.. I can't access the JavaScript file
Since the page you're accessing is at a different level than your master page, you want to make the script path relative to the root of your application
<script type="text/javascript" src="/javascript/main.js"></script>
EDIT if that's not working, then I would keep the script tag as it is above, run it in Chrome with the developer tools / network tab open, and look at the exact address that shows up for the failed request. Then look closely at your application and see what's different / wrong.
You can do:
<script type="text/javascript" src="<%=Page.ResolveClientUrl("~/javascript/main.js") %>" />
Allows you to use ASP.NETs ~. If this doesn't work then the file doesn't exist in that directory.

Print JavaScript code from external file

I am trying to understand how to include JavaScript externally so the code prints to the page.
When I insert the JavaScript directly into the page code, it prints "hello"
<html>
<head>
<title></title>
</head>
<body>
<script type="text/javascript">document.write("hello");</script>
</body>
</html>
However, when I put that same code into external file say "javascript.js" and include it (src) in the html it does not print "hello"?
<html>
<head>
<title></title>
<script type="text/javascript" src="http://thewebsite.com/javascript.js"></script>
</head>
<body>
</body>
</html>
I am trying to understand how to get that external JavaScript file to run and print "hello".
How does XSS work then if a hacker was to include the following tag inside say a textarea to call his malicious script from malicious server?
<script type="text/javascript" src="http://thewebsite.com/javascript.js"></script>
Heres whats in the "javascript.js" file:
<script type="text/javascript">
document.write("hello");
</script>
The file is on the same domain so Same Origin Policy should not apply here and as mentioned if I directly insert code it does work but not when I try to include as separate file.
I thought including JavaScript as external file, should print the contents of the external file (i.e. "hello" in this case) as if it was directly inserted in html page?
When I insert the JavaScript directly into the page code, it prints "hello"
Correct
However, when I put that same code into external file say "javascript.js" and include it (src) in the html it does not print "hello"?
If the content isn't being written then, presumably, an error is being thrown instead. Check the error console for your browser.
The problem is that you are including the HTML script tags in the JavaScript file. JavaScript files should contain only JavaScript.
The file is on the same domain so Same Origin Policy should not apply here
It doesn't. The Same Origin Policy just prevents JavaScript running (not loaded from) Origin A from reading data from Origin B. Since the data is included in the script itself, it would still be available, even if the script was loaded from Origin B.
I guess there is a policy enforced by browsers called Same Origin Policy which makes sure that JS from different domains does not access each others data when loaded in a single page. Lets say that you have a Google Ad and it has some Javascript in it. It wouldn't be advisable if the script in Google Ads be able to access the data in your site (Vice-Versa but ofcourse you always have Google Ads or the Like button as iFrame and hence anyways they are most neatly seperated.)
If you could load the js file as a src to image file then I suppose you can achieve what you intend to.(If I am not wrong.)
Edit: The javascript file cannot be given as input to the src of img tag. You can only use it as javascript: scheme.

Categories