Jump to content

Method to embed js contents in html page


SerenityNetworks

Recommended Posts

Is it possible to copy the contents of my "jquery.min.js" file into the actual html page code and reference it as the script resource? If so, how would I reference the src? Right now I use something like what's in the code block below. But I may need or find it simpler to actually have a single page footprint instead of having the library in a separate file.

<script type="text/javascript" src="js/jquery.js"></script>or <script type="text/javascript" src="https://drive.google.com/file/d/yaddayadda/view?usp=sharing"></script>

Thanks in advance,

Andrew

Link to comment
Share on other sites

You can copy the code and put it in a script block, but then you're increasing the size of every page because the browser will no longer cache files that you're using on multiple pages. The point of external CSS and Javascript files is so that the browser only needs to download them once, and then it can used the cached version.

Link to comment
Share on other sites

Yes, I understand that it won't cache. That's not an issue in this case, as this is a single page application.

 

Do I just put the library within its own set of script tags at the bottom of the page? When I copy the library and put in within "script" tags in the page will it automatically work or will I need to reference the page itself somehow? So maybe instead of...

<script type="text/javascript" src="js/jquery.js"></script>  //my code</script>

...I would need to put something like...

<script type="text/javascript" src="#"></script>  //my code</script>

I've never done this before, so I'm unsure of they syntax that should be used.

 

Thanks,

Andrew

Link to comment
Share on other sites

You don't use a src attribute with inline code, and the closing script tag goes after the code. You can replace any script tag that links to an external file with inline code at the same place and it will work the same.

That's not an issue in this case, as this is a single page application.

Surely you expect people to use the application more than once though, right? When you update jQuery or your application's code, you would rather search through a single large file than just replace individual Javascript files?
Link to comment
Share on other sites

Thanks.

 

Yes, I'd much rather use a separate file. But this is for a (very) simple mobile application. The mobile application creation tool I will likely end up using doesn't support the uploading and referencing of a separate file. I can provide an external reference, but then the application would only work when the user is online. So I'm stuck with the choices of having an application that can't be used offline, imbedding the js in the application's code, or finding another mobile application creation tool that allows for a separate js file. The ones I've found, so far, that allow for a separate file are either expensive or harder to set up and use than I care to get into for my simple little project. That's the reason I'm embedding the CSS and JS within the application page.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...