PHP code-igniter - javascript

I'm having troubles calling my external js files from my views php pages. The js files are saved in the "resources" folder in the root folder i.e "root/resources/scripts/myfile.js" just like the "root/application" and the "root/system" folder.
so in one of my php files in the views folder i am using this code:
<script type="text/javascript"
src="<?php echo base_url();?>resources/scripts/welcome.js">
and this php file is located in "root/application/views/welcome.php"
and still my page cannot link with the java script so i am stuck big time.
regards
k..

Try this:
<script src="<?= base_url('resources/scripts/myfile.js')?>"></script>

Looking at your question and the comments below your question, it seems you are using "resource" in one example and "resources" in another example.
Once fixed, view the source of the page (I use FireFox, as it makes the links clickable), search for "welcome.js". Once found, click on the link for that file. If you see javascript, it means the file is found, alternatively you will see a 404 error.

First of all check if the resource folder is available,
The java script file is available and the file naming is in the same small or capital case, and server has public permission of the file and folder.
by the way your code has missing end tag
</script>

Related

JavaScript path doesn't load on Live Server extension (VS Code)

HTML and CSS files are working perfectly on my live server. But every time I lead to a .js script it will not be shown on my live server. If I try to load the .js file directly through the URL it shows "Cannot GET /line.js". I already tried out everything I've found on the internet but it's still not working. Here are the points I checked/did:
Installed Code Runner
Installed Node.js = node.js system path done
Settings = Live Server Config = specified browser
"liveServer.settings.CustomBrowser": "chrome" on JSON settings
.js file is in a separate folder and accessed via <script src="line.js"></script> on index.html
Chrome is set as default browser on my system
Thanks for your inputs.
If the js file is in a separate folder, you need to provide the exact route to the folder in the script tag, since in the current form it is trying to find the js file in the root directory. The script tag should look like this:
<script src="FOLDER_NAME/line.js"></script>
It's possible that your javascript file is being loaded before the HTML page is rendered. You can try adding "defer" to your script tag like this:
<script src="demo_defer.js" defer></script>

How to refer javascript file in html present in another folder

I have a JS file in folder
/admintool/src/main/webapp/static/js/itemInventory.js
i have html file in folder
/admintool/src/main/webapp/WEB-INF/templates/itemInventory.ms
On page load my starts with url - http://localhost:8080/admintool/selectItem/showAllItems
Inside itemInventory.ms file, when i use the below code to refer that javascript, it is not able to detect it.
<script src="static/js/itemInventory.js"></script>
In browser console, i get an error
GET http://localhost:8080/admintool/selectItem/static/js/itemInventory.js net::ERR_ABORTED
How do i specify the location of this JS file properly ?
Are you sure, that the links are right? The link from the HTML to the JavaScript have to be:
../../static/js/itemInventory.js
Ivo

How do I link JS external files outside of my PHP project root directory?

On my site I have my resources folder outside of the root, for example:
/var/www/html/ is the root directory
/var/www/resources/
I currently have a config file that sets the location of the library so I can include it with php like so:
defined("LIBRARY_PATH")
or
define("LIBRARY_PATH", realpath(dirname(__FILE__) . '/library'));
which works perfectly when I use:
<?php include_once(LIBRARY_PATH . "/file.php"); ?>
but it doesn't work when trying to add Javascript files:
e.g.
<script src="../resources/library/js/test.js"></script>
links to 'www.website.com/resources/library/js/common.js'
or
<script src="<?php echo LIBRARY_PATH; ?>/js/test.js"></script>
links to 'www.website.com/var/www/resources/library/js/test.js'
neither of which work.
Any suggestions on how I can do this without having the js files in or above the root?
Your JavaScript files have to be accessible to the browser because they are executed by the browser and not by the server.
This requires that they have a URL.
Putting the files under the webroot is the standard way to give a static file a URL.
Alternatively, you could write a program (e.g. in PHP) that will read the file and then output it's content to the browser. This is more complicated and makes dealing with cache control headers more fiddly and is not recommended.
Assuming you understand what you're doing and security implications of that!..
You create the linkjs.php script that takes the relative path to the script (from some root dir, perhaps /var/www/resource/js) as a parameter, like:
<script src="/linkjs.php?p=test.js">
In your PHP script you resolve the full file path, check that it's indeed a file under the root dir (to protect against ../ in the parameter), that it's readable by you PHP user, read the content and output it into the response. Don't forget to set content type to text/javascript of course.
Ideally, you should also provide proper caching headers based on the source file modification time, but that is a topic in itself. See the guidelines in other SO questions about proper caching headers for dynamic content.
The upside is that you can do on-the-fly script minification/combining/wrapping/substitutions if you like/need.

Refrer javascript inside html file?

I have below folder structure.
inside: webapp/WEB-INF/some.jsp i have
i have javascript file in the same webapp/WEB-INF/js/myform.js location
i referred it in some.jsp as below:
<script type="text/javascript" src="js/myform.js"></script>
But it is not finding javascript file. in viewsource i am getting below lines:
<title>Error 404 NOT_FOUND</title>
</head>
<body><h2>HTTP ERROR: 404</h2><pre>NOT_FOUND</pre>
<p>RequestURI=/js/myform.js</p><p><i><small>Powered by Jetty://</small></i></p><br/>
is my javascript link correct in jsp file?
Please correct me.
Thanks!
The WEB-INF folder is secure, meaning you can't access resources placed in it directly using a URL from the browser.
To work around it, place the JS under the webapp/js directory.
webapp/WEB-INF is not the root of your application, put your dir js in webapp rather than in webapp/WEB-INF

Linking to javascript in a view subdirectory?

I have a view folder called 'HelloWorld', inside it I have another folder called 'table' with stuff inside it, I have something located here: table/media/js/jquery.js under the helloworld directory, I've set it to 'copy always' and its build action i s'Content'.
In the index of the view control I do this:
<script type="text/javascript" language="javascript" src="table/media/js/jquery.js")></script>
But for some reason it can't find it, if I try to manually go to it via the URL (i.e. http://localhost:XXXX/HelloWorld/table/media/js/jquery.js) the server still can't find it.
Any ideas how I'm supposed to make this work?
The View folder is not browsable for security reasons, then you cannot serve files like your javascript file.
See the web.config inside the View folder, the handler BlockViewHandler does this,
create a similar structure within the content folder if you need to organize files.
Assuming you have created the following folder: ~/HelloWorld/table/media/js/jquery.js where ~ is the root of your web site I would recommend you using url helkpers in order to generate proper url:
<script type="text/javascript" src="#Url.Content("~/HelloWorld/table/media/js/jquery.js")")></script>
On the other hand the Views folder is a special folder in which you should put only your MVC views and not static files such as css and javascript that are intended to be served directly on the client.

Categories