Javascript doesn't work if I invoke it as URL - javascript

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.

Related

Stop cloudflare injecting HTML into my code

I have a few pieces of example JS and HTML that are stored in .js and .html files on my server. My server is run using cloudflare as a DNS server (I'm hosting it on my own server).
I read this code to display using code-prettify in my code, and it works in my local machine. When I push this code to my server, there are a few extra elements and additions to elements in my html.
This is the html file, and how it shows in the browser on my localhost
<!DOCTYPE html>
<html>
<script src="../../libraries/p5.min.js"</script>
<script src="../../libraries/p5.graphing.js"></script>
<script src="xySample.js"</script>
</html>
This is the html that displays when my server is through cloudflare
<!DOCTYPE html>
<html>
<script src="../../libraries/p5.min.js" type="5679b6186495861c611c81b4-text/javascript"></script>
<script src="../../libraries/p5.graphing.js" type="5679b6186495861c611c81b4-text/javascript"></script>
<script src="xySample.js" type="5679b6186495861c611c81b4-text/javascript"></script>
<script src="https://ajax.cloudflare.com/cdn-cgi/scripts/a2bd7673/cloudflare-static/rocket-loader.min.js" data-cf-settings="5679b6186495861c611c81b4-|49" defer=""></script>
</html>
On my cloudflare dashboard, I've been to the Page Rules tab and have passed in the url of both the HTML which my code should show up in as well as the location of the HTML thats being injected and disabled rocket loader for both. This didnt work, even after clearing cookies.
How do I stop this HTML injection by cloudflare. Thanks!
Just disable Rocket Loader in your Cloudflare dashboard

How to import codes in .html file which are placed in a txt file ?

So, what i want is this. I have an HTML File and i want the code to load from a text file. So, the browser should get the code from that text file and read it as a part of the HTML code. Here is an example:
Suppose I have an HTML File whose code is :-
<html>
<head></head>
<body>
<div src="code.txt"></div> [ I made this code. I know this code does not exist.]
</body>
</html>
And i have text file named 'code.text' :
<h2>Welcome</h2>
<img src="sample.jpg">
<p>This is the paragraph.</p>
So, instead of writing code in the actual HTML File, i saved the codes in a text file and want to use them whenever i want. I know the following code does not exist.
<div src="code.text"></div>
I made up this code just to show what i want.
**
Just tell me the way i can achieve this purpose.
**
Thanks,
Waiting for your answer !
You can load the content of this with jQuery ajax. Your div will look like this: <div id="content"></div>
And you call the document using ajax:
$.ajax("code.text").done( html =>{ $('#content').html(html) });
You can view more on http://api.jquery.com/jquery.ajax/
Why not use SHTML?
SHTML is an HTML file that includes server instructions or server side includes and is similar to an ASP file.
It's embedded in a standard XML comment, and looks like this:
<!--#include virtual="../top.shtml" -->
<!--#include virtual="../quote.txt" -->
For example, a .shtml page that includes a resource can be as follows:
<!doctype html>
<html lang="en">
<head>
<title>My SHTML Page</title>
</head>
<body>
<!--#include virtual="content.html" -->
</body>
</html>
You can use to include a common header and footer in your pages, so you don't have to repeat code as much, and changing one included file updates all of your pages at once.
Apache can be configured to parse any file with a particular file extension, such as .shtml, with the following directives in the http.conf file:
AddType text/html .shtml
AddHandler server-parsed .shtml
I got the SOLUTION.
And the solution is to use "php include".
<?php include("code.text"); ?>
OR similarly you can use it for other files.
<?php include("your_html_code.html"); ?>
<?php include("your_html_css.css"); ?>
You can import any code files like this (text,html,css etc.) and the browser will read the codes as a part of the main code.
This way you can use it to create templates so that you don't have to type the same code again and again.
Also, if you intend to make some changes, then the changes will be applied everywhere. This will save you a lot of time if you are creating a blog or if your website has many pages with some same sections.
Get the full guide for using this technique of php 'include' here:
http://www.apaddedcell.com/how-automatically-include-your-header-navigation-and-footer-every-page

Why won't my .html file connect to .js file?

I want to connect my .html to .js. I'm trying to run this simple program but it's not working. Below is the screenshot of my file path and files I'm working with.
Here's map.html:
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
</body>
<script type="javascript" src="map.js"></script>
</html>
Here's map.js:
document.write("testing");
The problem is below. How can I render the .js file along with the .html file?
Here's views.py:
def map(request):
return render(request, 'personal/map.html')
A common CMS convention is to save JavaScript files in a static folder.
You save anything that you don't want your template engine messing with there: images, javascript, css, etc.
It looks like you may need to save map.js at this path:
mysite/personal/static/personal/js/map.js
After that, you'll need to update you script link in your HTML to something like:
<script src="static/js/map.js">
The src path here isn't relative to where you store the file on your computer, but to the URI that your web server associates with it.
Depending on how you've set things up, you'll need some portion of the new path.
Django has a few ways of linking to static resources, but I'm not familiar enough with the platform to tell you which option you should use.

launching an external javascript file syntax

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

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