Need to include the parameter in javascript url in htmls - javascript

I have an requirement where I need to pass the parameter dynamically in script url.
Example:
"<" script src="myfile.js" "><"/script>"
now I need to pass the timestamp in the src like
"<"script src="myfile.js?tm=12345"><"/script">"
so whenever the this html load it should always take the new timestamp and pass in to the url.
"<"script src="myfile.js?tm=6788"><"/script">"
I would really appreciate in case some can help me to find the solution to this problem.

You can do something like this:
var elem = document.createElement('script');
elem.setAttribute('src',generateSrc()); // add your logic in generateSrc
document.body.appendChild(elem);

To create dinamically your tag with timestamp, you can do it in two different ways:
By using DOM:
var e = document.createElement('script');
e.setAttribute('src',"myfile.js?tm="+(new Date()).getTime()); //(new Date()).getTime() Returns the number of milliseconds since midnight Jan 1, 1970
document.getElementsByTagName("body")[0].appendChild(e);
By using innerHTML:
document.getElementsByTagName("body")[0].innerHTML+= '<script src="myfile.js?tm='+(new Date()).getTime()+'"></script>';
Here's http://cjihrig.com/blog/passing-arguments-to-external-javascript-files/ a tutorial that explain how to pass and retrive arguments to an external script.

Related

Multiple attribute on href using onclick

I Tried this code to get multiple value in href but it does not work. any problem on this one ?
Print
You are missing a + sign between a string and a value.
The error is between this two
document.getElementById('CUS_CODE_MX').value '&AGE='
Correct format
document.getElementById('CUS_CODE_MX').value + '&AGE='
Every time you join a value and a string, you need a + sign
Even if you are joining two strings
'Hello'+ 'World'
Pliss avoid long js as an inline atribute. I will recommend you call a function as the onclick attribute.
Hope this helps :)
Print
It's better to use external script for that rather than inline format. And just add missing + to your code. Also, using variables would clean up the code.
function func() {
var CUS_CODE_MX = document.getElementById('CUS_CODE_MX').value;
var AGEID = document.getElementById('AGEID').value;
this.href = 'printsales.php?CUSTOMERID='+CUS_CODE_MX+'&AGE='+AGEID;
}
Print

When try to pass date the slashes occur division

I have a variable var that contains a date "29/5/2017" in my page. Now i am trying to pass this variable to a function that is stored in a separate js file. My issue is when i debug the js i see a number 0,0028... The js believes that this is a number a makes a division. how can i prevent this and pass just the date?
the var in my aspx file is :
var mydates = '29/5/2017';
the function call in my aspx file
calldate(mydates);
the function in the js file
function calldate(mydates) {
alert(mydates);
}
you need to use double quotations to single quotations mark during init variable
var mydates = "29/5/2017".toSting();
and user .toString() function for canvart to string.
Thanks
Instead of passing date as string try to pass variable as Date datatype.
there might be chances that the variable getting changed before calling the function.
<pre>
var mydates = Date("29/5/2017");
</pre>

jQuery function only being run once

Using the Moments.js library I'm attempting to grab the value of the datetime attribute from every time tag and output the time using natural language.
Given this:
<time datetime="2014-06-27"></time>
The Results would be:
<time datetime="2014-06-11">17 hours ago</time>
Using this:
$(document).ready(function(){
var time = $('time'),
date = moment(time.attr('datetime')),
update = function(){
time.html(date.fromNow());
};
update();
});
It works, but only for the first time element. All the additional time elements are also saying "17 hours ago".
It's my guess that the function is only being run once on the first instance of time.
How can I make sure it's being run for each, and doing so efficiently?
You need to loop through all the time tags. Right now you are only applying it to the first instance of <time>. Use jQuery .each().
$(document).ready(function(){
var time = $('time');
time.each(function() {
date = moment($(this).attr('datetime'));
$(this).html(date.fromNow());
});
});
jsFiddle Demo

add javascript datetime inside a html url

For a cache issue, I want to send the current datetime with a url, but with the following code, the current timestamp isn't shown in the url..
<script>var d=new Date();</script>
<li><a href="/controllername?t="+d.getTime()+"&name=.... // etc
How to add javascript datatime properly in the url?
Instead of using inline javascript a better approach would be to identify your element in the DOM and then simply set it's href attribute like the following:
html
<ul>
<li><a id="timeLink" href="#">​​​​​​​​​​​​​​​​​​Time Link</a></li>
</ul>​​​​​​
javascript
var d=new Date();
var timeLink = document.getElementById("timeLink");
timeLink.setAttribute("href","/controllername?t="+d.getTime());
As usual, using jQuery can simplify a lot:
$(document).ready(function(){
var d = new Date();
$("#timeLink").attr("href","/controllername?t="+d.getTime());
});​
Here's a functional fiddle
Try this:
click me
Use new Date().getTime(); and attach it to the link
<a id="link" href="/controllername?t="+d.getTime()+"&name=.... // etc
by grabbing the link element and appending the timestamp:
var link = ​document.getElementById('link');​​
var now = new Date().getTime();
link.setAttribute("href", "/controllername?t=" + now);
Since you want to add the current timestamp when you click on a link, I would attach the above code to the onclick function of the href, so when you click on the link the actual timestamp will be appended to the url. See the example http://jsfiddle.net/GBBdc/.
Since your using ASP You can define your script as this. You can do any format you like (.ToString("hh:mm:ss") ) for example
<script> var d = '<%=DateTime.Now.Ticks %>';</script>
This way you can have any format and use it backend any way you like, always ensuring you have the correct format, as apposed to client side strings which may be in a localized format
You cannot use the d object if you are not inside <Script>
do something like:
<script>
var d=new Date();
$("#mylink").attr("href","...." + d.getTime() + "..");
</script>
<a id="mylink">
Very easy just write this in your script
var d = '';
Time gets changed with every second, millisecond etc. so you need to send most recent time value with url:
Send Request
Javascript:
function sendRequest(oldUrl){
var d = new Date();
var newUrl = oldUrl + d.getTime();
window.location = newUrl;
}
Here's is its DEMO

Given a string of html code, how can I go through every tag and remove ones that is not in my whitelist (in JQuery)?

var whitelist = ['a','div','img', 'span'];
Given a block of HTML code, I want to go through every single tag using JQuery
Then, if that tag is NOT in my whitelist, remove it and all its children.
The final string should now be sanitized.
How do I do that?
By the way, this is my current code to remove specific tags (but I decided I want to do whitelist instead)
var canvas = '<div>'+canvas_html+'</div>';
var blacklist = ['script','object','param','embed','applet','app','iframe',
'form','input', 'link','meta','title','input','button','textarea'
'head','body','kbd'];
blacklist.forEach(function(r){
$(canvas).find(r).remove();
});
canvas_html = $(canvas).get('div').html();
Try this:
var whitelist = ['a','div','img', 'span'];
var output = $('<div>'+canvas_html+'</div>').find('*').each(function() {
if($.inArray(this.nodeName.toLowerCase(), whitelist)==-1) {
$(this).remove();
}
}).html();
// output contains the HTML with everything except those in the whitelist stripped off
try:
$(canvas).find(':not(' + whitelist.join(', ') + ')').remove().html();
The idea is to turn array of whitelist into "el1, el2, el3" format, then use :not selector to get the elements that's not in the whitelist, then delete.
This obviously could be expensive depending on the size of your html and whitelist.
Unfortunately, using jQuery to sanitize HTML in order to prevent XSS is not safe, as jQuery is not just parsing the HTML, but actually creating elements out of it. Even though it doesn't insert these into the DOM, in some cases embedded Javascript will be executed. So, for example, the snippet:
$('<img src="http://i.imgur.com/cncfg.gif" onload="alert(\'gotcha\');"/>')
will trigger an alert.

Categories

Resources