Avoiding call for the Js file which has been already included? - javascript

i hav included below js file in my jsp
<script type="text/javascript" src='<s:url value="/script/customer.js?ver=1.2.3"/>'></script>
Jsp file contains hyperlink on which ajax call is made and response of that call contains again above code to include js file.
Ideally no call should be made for the same js file again they are cached by browser.Then i inspected in firebug and found out
below url is bit diefferent when i.e it includse extra parameter _=1360589976328. I do not want to make the call for the
same js file again if it has been included.(i can not remove from ajax response jsp because of some reason). I think problem
is this extra paramter, probably becoz of which call is made again.
Any idea why extra param is appended or double call is
made to include the js file when it already exists in browser?
http://localhost:8080/custModule/script/customer.js?ver=1.2.3?ver=4.5.8&_=1360589976328

Related

How can i use Ajax request on multiple files?

I have a JavaScript file called newsletter_init.js and it makes an Ajax request.
I add this JS file to every .php on my project and the issue here.
For example I use newsletter_init.js in my index.php and a URL for the ajax request will be res/ajax/request_newsletter.php.
Ok, that's working but when I add the same JS file on another directory for example adding newsletter_init.js on a index.php but placed on blog folder the URL will be invalid as it will search for ../res/ajax/request_newsletter.php instead of res/ajax/request_newsletter.php.
File structure about the scope:
index.php
blog > index.php
I have another way to do this:
Add content of newsletter_init.js on every file and change url but this is not a good way to approach this.
I have tried to use __DIR___ in PHP but i don't understand how to use it in this situation.
I have tried $(location).attr(pathname); in jQuery but it return the fild name also for example localhost/homepage/index
I can't use a direct url as i will change from localhost to my site so, i need a dynamic way.
Just ensure your path is relative to the root directory.
/res/ajax/request_newsletter.php
Not:
res/ajax/request_newsletter.php
If you don't add the first slash, it will try to find that path starting in the directory where you are making the call (i.e. wherever your current PHP script is running).

JavaScript file doesn't effect new file imported via jquery load() method

I have a file index.php and there is a main js script in its header.
I'm using Jquery load method to load other php file inside a div element:
$("#formHideinAjax").load('loginpass.php');
And as our friend explained here the main js file which had loaded in the header doesn't work in new imported file so I was forced to add the js file inside the loginpass.php too(Now I have two same js file, one in header and one in div that loads loginpass.php ! )
I know that this method of loading js file is not standard and sends more request to my server.
How can I fix this problem?
Well, it is explicitly said in $.load() documentation. I'm afraid you can't just paste <script></script> code into your DOM for browser to run it.
I'm afraid you have to send JavaScript code from your loginpass.php file (without <script></script> tags) and just eval it in your JavaScript
$("#formHideinAjax").load('loginpass.php', function () {
// eval JavaScript code from php file here
});

Jade: How to include js file in a certain point

i want to include a file that must be used only when a jade file is render client side, this is the .jade file:
button(type="button")#start.flex
p#timer.flex
button(type="button" )#stop.flex
script(src='../public/js/timer.js')
The js file will handle the timer function, i have a console log in the .js file but it is never fired. What i'm doing wrong? I canno't import it in the head because the element won't be ready yet.
Thus i have to find a way to include and use timer.js only when this file is render. Thank you guys
JavaScript inserted as DOM text will not execute.
You must load the script file initially, with the document load. When loading content via AJAX response, execute whatever javascript function or functions you want in the success handler. There are other ways as well, but your current design wont work.
Checkout this link:
http://caih.org/open-source-software/modularjs/loading-javascript-script-vs-ajax-vs-both/

Is it possible to insert an include tag into a webpage using JavaScript?

I want to insert the following include tag into my webpage using JavaScript. <!--#include virtual='includes/myIncludeFile.htm' -->
I have tried the following but it doesn't work: jQuery("<!--#include virtual='includes/myIncludeFile.htm' -->").appendTo(jQuery("body"));
I have outputted jQuery("<!--#include virtual='includes/myIncludeFile.htm' -->") to the console and it thinks that it is a comment object (see screenshot).
Where am I going wrong and how can it be done?
The basic premise of what you're trying to do isn't going to work. The HTML "include tags" you're using are also known as "server-side includes." That is, they are processed on the server before the page is sent to the client.
By the time the JavaScript code is executing on the client, the server is already processing the response. The client-side code can't initiate a server-side include.
One thing you can do from the client-side is use something like jQuery's .load() function to make a request to the server and load the response into a specified element on the page. Something like this:
$('#includeDiv').load('includes/myIncludeFile.htm');
This would dynamically load all of the contents from myIncludeFile.htm into an element of id "includeDiv" on the page.

Call javascript code on page using PageAsyncTask

I have two ASPX pages (P1.aspx and P2.aspx). The first (P1.aspx) contains a lot of JavaScript code.
How can I call all this JavaScript from another page (P2.aspx)?
I tried to do this using PageAsyncTask from code behind of P2.aspx, but JavaScript code (on P1.aspx) didn't work.
Any suggestions?
you cant do that.
put the javascript into JS file and reference it when needed.
What you can do ( I think) is to get the HTML content of the file and then EXTRACT the JS data
edit
try that (I dont think that it will include the inside JS - but try it yourself)
WebRequest oRequest;
WebResponse oResponse;
oRequest = WebRequest.Create("http://www.google.com/");
oResponse = oRequest.GetResponse();
StreamReader sr = new StreamReader(oResponse.GetResponseStream());
string pagedata = sr.ReadToEnd();
pagedata+=#"sdfsdf";
All javascript code you want to use on a page has to be included in that page or dynamically loaded by that page. You cannot call code that is only in another page.
The usual way of sharing code among pages is to have a .js file that contains common code that is included in more than one page and then a .js file that contains code that is unique to each specific page (if required).
One way to do this is <!--#INCLUDE FILE="somefile.aspx" --> but your aspx page will complain that there can be only one 'page' directive. So in order to do this properly you need to include as the previous mentiond the js files in the aspx file.
One way is makeing a master page and include all the necessary js code in there, then all pages that are loaded within the masterpage will automatically inherit the javascript included libraries.
Another way is to make an html file that includes all the libraries and then use the <!--#INCLUDE FILE="myjslibs.html" --> to include all your code there to each page.
So actually copy all your code in one file and then include that just one file each time in every page.

Categories

Resources