Function Conflict with Jquery.min.js - javascript

currently working on metal website so i need unit conversion tool like in this site
http://www.theunitconverter.com/unit-conversion-widget.html
i try add the code, but its not working.
and its working when i remove this
<script src="https://code.jquery.com/jquery-2.2.1.min.js"></script>
the problem is i need those min.js script for another jquery function.
and i try to add the javascript converter to another file but i couldn't place the correct noconflict function. the js goes like this.
function IsNum(a) {
if (a != null && a != "") {
return !isNaN(a)
}
return false
}
function $(b) {
if (typeof b == "string") {
return document.getElementById(b)
}
return b
}
function v(g) {
var f = $("a").value;
var e = $("b").value;
var j = $("c").value;
var h = $("d").value;
if (g == 0) {
if (!IsNum(f)) {
return false
}
$("c").value = $("a").value * h / e
} else {
if (!IsNum(j)) {
return false
}
$("a").value = $("c").value * e / h
}
}
function o(a) {
var j = $("e").value;
var d = $("f").value;
var c = $("g").value;
var b = $("h").value;
if (a == 0) {
if (!IsNum(j)) {
return false
}
$("g").value = $("e").value * b / d
} else {
if (!IsNum(c)) {
return false
}
$("e").value = $("g").value * d / b
}
}
function r(x) {
var q;
var p;
var ub;
var ud;
var s;
if (x == 1) {
ub = $("b");
ud = $("d");
ua = $("a");
uc = $("c");
s = $("u").value
} else {
ub = $("f");
ud = $("h");
ua = $("e");
uc = $("g");
s = $("s").value
}
var f = eval(s + "f");
var t = eval(s + "t");
var u0 = eval(s + "0");
var u1 = eval(s + "1");
var amount = eval(u0)[t] / eval(u0)[f];
var z = 0;
for (var n in u) {
if (s == u[n]) {
z = 1
}
}
ua.value = "1";
uc.value = amount;
ub.options.length = 0;
ud.options.length = 0;
if (z == 1) {
var o = new Option("Common units", "0");
o.disabled = "false";
var r = new Option("Common units", "0");
r.disabled = "false";
ub.add(o);
ud.add(r);
for (var n in u0) {
p = new Option(n, u0[n]);
if (n == f) {
p.selected = "true"
}
q = new Option(n, u0[n]);
if (n == t) {
q.selected = "true"
}
ub.add(p);
ud.add(q)
}
o = new Option("Other units", "0");
o.disabled = "false";
r = new Option("Other units", "0");
r.disabled = "false";
ub.add(o);
ud.add(r);
for (var n in u1) {
p = new Option(n, u1[n]);
q = new Option(n, u1[n]);
ub.add(p);
ud.add(q)
}
} else {
for (var n in u0) {
p = new Option(n, u0[n]);
q = new Option(n, u0[n]);
if (n == f) {
p.selected = "true"
}
q = new Option(n, u0[n]);
if (n == t) {
q.selected = "true"
}
ub.add(p);
ud.add(q)
}
}
};
Thanks for any kind of help.

Wrap everything inside
(function($) {
...
})(jQuery);
Your code should be
(function($) {
function IsNum(a) {
if (a != null && a != "") {
return !isNaN(a)
}
return false
}
function $(b) {
if (typeof b == "string") {
return document.getElementById(b)
}
return b
}
function v(g) {
var f = $("a").value;
var e = $("b").value;
var j = $("c").value;
var h = $("d").value;
if (g == 0) {
if (!IsNum(f)) {
return false
}
$("c").value = $("a").value * h / e
} else {
if (!IsNum(j)) {
return false
}
$("a").value = $("c").value * e / h
}
}
function o(a) {
var j = $("e").value;
var d = $("f").value;
var c = $("g").value;
var b = $("h").value;
if (a == 0) {
if (!IsNum(j)) {
return false
}
$("g").value = $("e").value * b / d
} else {
if (!IsNum(c)) {
return false
}
$("e").value = $("g").value * d / b
}
}
function r(x) {
var q;
var p;
var ub;
var ud;
var s;
if (x == 1) {
ub = $("b");
ud = $("d");
ua = $("a");
uc = $("c");
s = $("u").value
} else {
ub = $("f");
ud = $("h");
ua = $("e");
uc = $("g");
s = $("s").value
}
var f = eval(s + "f");
var t = eval(s + "t");
var u0 = eval(s + "0");
var u1 = eval(s + "1");
var amount = eval(u0)[t] / eval(u0)[f];
var z = 0;
for (var n in u) {
if (s == u[n]) {
z = 1
}
}
ua.value = "1";
uc.value = amount;
ub.options.length = 0;
ud.options.length = 0;
if (z == 1) {
var o = new Option("Common units", "0");
o.disabled = "false";
var r = new Option("Common units", "0");
r.disabled = "false";
ub.add(o);
ud.add(r);
for (var n in u0) {
p = new Option(n, u0[n]);
if (n == f) {
p.selected = "true"
}
q = new Option(n, u0[n]);
if (n == t) {
q.selected = "true"
}
ub.add(p);
ud.add(q)
}
o = new Option("Other units", "0");
o.disabled = "false";
r = new Option("Other units", "0");
r.disabled = "false";
ub.add(o);
ud.add(r);
for (var n in u1) {
p = new Option(n, u1[n]);
q = new Option(n, u1[n]);
ub.add(p);
ud.add(q)
}
} else {
for (var n in u0) {
p = new Option(n, u0[n]);
q = new Option(n, u0[n]);
if (n == f) {
p.selected = "true"
}
q = new Option(n, u0[n]);
if (n == t) {
q.selected = "true"
}
ub.add(p);
ud.add(q)
}
}
};
})(jQuery);

Related

Javascript string decoding logic translated to Python

The following Javascript code will decode string 91ebf9e9f7a8a2a1a5d1a0a3a7bff2fefc
into an intelligible value:
!function() {
"use strict";
function e(e) {
try {
if ("undefined" == typeof console)
return;
"error"in console ? console.error(e) : console.log(e)
} catch (e) {}
}
function t(e) {
return d.innerHTML = '',
d.childNodes[0].getAttribute("href") || ""
}
function r(e, t) {
var r = e.substr(t, 2);
return parseInt(r, 16)
}
function n(n, c) {
for (var o = "", a = r(n, c), i = c + 2; i < n.length; i += 2) {
var l = r(n, i) ^ a;
o += String.fromCharCode(l)
}
try {
o = decodeURIComponent(escape(o))
} catch (u) {
e(u)
}
return t(o)
}
function c(t) {
for (var r = t.querySelectorAll("a"), c = 0; c < r.length; c++)
try {
var o = r[c]
, a = o.href.indexOf(l);
a > -1 && (o.href = "mailto:" + n(o.href, a + l.length))
} catch (i) {
e(i)
}
}
function o(t) {
for (var r = t.querySelectorAll(u), c = 0; c < r.length; c++)
try {
var o = r[c]
, a = o.parentNode
, i = o.getAttribute(f);
if (i) {
var l = n(i, 0)
, d = document.createTextNode(l);
a.replaceChild(d, o)
}
} catch (h) {
e(h)
}
}
function a(t) {
for (var r = t.querySelectorAll("template"), n = 0; n < r.length; n++)
try {
i(r[n].content)
} catch (c) {
e(c)
}
}
function i(t) {
try {
c(t),
o(t),
a(t)
} catch (r) {
e(r)
}
}
var l = "/cdn-cgi/l/email-protection#"
, u = ".__cf_email__"
, f = "data-cfemail"
, d = document.createElement("div");
i(document),
function() {
var e = document.currentScript || document.scripts[document.scripts.length - 1];
e.parentNode.removeChild(e)
}()
}();
Can anyone explain the actual logic of it, to be able to write it into Python? Or come up with an JS2PY solution of running the code against python-selected values?
You can use js2py module to rewrite automatically the Js code to Python:
import js2py
js_script = """\
function decode(email) {
function r(e, t) {
var r = e.substr(t, 2);
return parseInt(r, 16);
}
function n(n, c) {
for (var o = "", a = r(n, c), i = c + 2; i < n.length; i += 2) {
var l = r(n, i) ^ a;
o += String.fromCharCode(l);
}
return o;
}
var l = "/cdn-cgi/l/email-protection#";
return n(email, email.indexOf(l) + l.length);
}
"""
decoder = js2py.eval_js(js_script)
email = decoder(
"https://journals.sagepub.com/cdn-cgi/l/email-protection#7c0614041a454f4c483c4d4e4a521f1311431f1f414d454c454c494b4a4d3c0d0d521f1311"
)
print(email)
Prints your email.

Export JS fuction to use in other JS not working

This is my the my js:
//aca Z
var Z = (function () {
var i = "#z-overlay {filter: alpha(opacity=50);opacity:0.5;display:inline-block;position:fixed;top:0;left:0;width:100%;height:100%;background-color: #000;z-index: 1001;}";
var u =
"#z-container {border:1px;float:left; overflow: visible; position: absolute;padding: 0px; display: inline-block; top:5%; left:34%; margin: 0 auto;-webkit-border-radius: 5px;-moz-border-radius: 5px;border-radius:5px;background-color: #FAFAFA; border:1px solid #FAFAFA;border-top-color:#EDEDED;behavior: url(js/PIE.htc);z-index: 1002;}";
var h = "#z-data {height: 100%; outline: 0px; width: 100%; overflow: visible;display: inline-block;border:1px; -webkit-border-radius: 5px;-moz-border-radius: 5px;border-radius:5px;}";
var t = "#reset{*, *:before, *:after {display: inline-block;-webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box;}}";
var l = "#z_hppm_iframe {background-color: #FAFAFA;vertical-align:bottom;z-index:9999;display:block;padding:0px;margin: 0px; border:0px solid #DDD;}";
var e = "requestPage";
var d;
var s;
var c, o;
var b;
var n;
var w = ["tenantId", "id", "token", "signature", "key", "style", "submitEnabled", "url"];
var j = ["creditCardNumber", "cardSecurityCode", "creditCardExpirationYear", "creditCardExpirationMonth", "bankAccountNumber", "bankAccountName", "ipAddress", "achBankAccountNumber", "achBankABACode"];
var r = false;
var m = {};
var f = null;
var v = null;
function a(x) {
return x && typeof x === "function";
}
function p(x) {
var y = {};
for (var z in x) {
if (x.hasOwnProperty(z)) {
y[z] = x[z];
}
}
return y;
}
function k() {
if (s) {
s();
}
}
function g() {
s = null;
}
function q(x) {
if (x.currentStyle) {
return x.currentStyle.display != "none";
} else {
if (document.defaultView && document.defaultView.getComputedStyle) {
return document.defaultView.getComputedStyle(x, null).getPropertyValue("display") != "none";
} else {
return true;
}
}
}
ZXD.receiveMessage(function (y) {
try {
var x = y.data;
ZLOG.debug("Received message[O]: payload=" + x);
x = JSON.parse(x);
if (x.success) {
if (a(m.init)) {
m.init(x);
}
} else {
if (x.success == false) {
Z.deactivateOverlay("z-overlay");
Z.deactivateOverlay("z-container");
if (m.init) {
m.init(x);
}
} else {
if (x.action == "close") {
Z.deactivateOverlay("z-overlay");
Z.deactivateOverlay("z-container");
} else {
if (x.action == "resize") {
Z.receive(x);
} else {
if (x.action == "allowScroll") {
Z.allowScroll(x);
}
}
}
}
}
if (x.action === "validate" && a(m.validate)) {
m.validate(x);
} else {
if (x.action == "customizeErrorMessage" && a(m.customizeErrorMessage)) {
m.customizeErrorMessage(x.key, x.code, x.message, x.rawGatewayInfo);
} else {
if (x.action && a(m[x.action])) {
m[x.action](x);
}
}
}
} catch (z) {
return;
}
});
return {
validateRequiredParams: function (z) {
var x = w.length;
for (index = 0; index < x; index++) {
if (!z.hasOwnProperty(w[index])) {
if (w[index] == "submitEnabled" && z.style.toLowerCase() == "overlay") {
continue;
} else {
var y = "Param with key [" + w[index] + "] is required.";
alert(y);
if (!Z.isIE()) {
console.log(y);
}
return false;
}
}
}
return true;
},
isIE: function () {
var z = window.navigator.userAgent;
var y = z.indexOf("MSIE ");
var x = z.indexOf("Trident/");
if (y > 0) {
return true;
}
if (x > 0) {
var A = z.indexOf("rv:");
return true;
}
return false;
},
validatePCIParams: function (A) {
var x = j.length;
for (index = 0; index < x; index++) {
if (j[index] == "achBankAccountNumber" || j[index] == "achBankABACode") {
return true;
}
var y = "field_" + j[index];
if (A.hasOwnProperty(y)) {
if (0 < A[y].trim().length && A[y].trim().length < 300) {
var z = "Field [" + y + "] for Credit Card payment method type should be encrypted for pre-population";
alert(z);
if (!Z.isIE()) {
console.log(z);
}
return false;
}
}
}
return true;
},
init: function (C, D) {
c = "?method=requestPage&host=" + encodeURIComponent(document.location.href) + "&";
c = c + "fromHostedPage=true&";
var B = Z.validateRequiredParams(C);
if (!B) {
return false;
}
B = Z.validatePCIParams(C);
if (!B) {
return false;
}
var A = JSON.stringify(C, function (F, G) {
if (F != "") {
if ("key" == F) {
b = G;
} else {
if ("url" == F) {
var E = /^https:\/\/([a-z0-9]+(-[a-z0-9]+)*\.)+zuora\.com\/.+$/;
ZLOG.info("HPM integration[O]: url=" + G);
if (!E.test(G)) {
ZLOG.warn("HPM integration[O]: None-official zuora HPM integration url is detected: " + G);
}
d = G;
} else {
c = c + F + "=" + encodeURIComponent(G) + "&";
}
}
}
return G;
});
n = JSON.parse(A);
m.init = D;
var x = j.length;
if (C) {
for (var z = 0; z < x; z++) {
var y = "field_" + j[z];
if (C.hasOwnProperty(y)) {
C[y] = "";
}
}
}
return true;
},
prepopulate: function (z) {
if (threedRedirected) {
return;
}
var A = Z.createIframeURL();
if (A == document.getElementById(ifrmId).src || (document.getElementById(ifrmId).src.indexOf(A) >= 0 && n.hasOwnProperty("customizeErrorRequired") && n.customizeErrorRequired == "true")) {
var x = JSON.stringify(z, function (B, D) {
if (B != "") {
var C = "setField(" + B + ":" + D + ")";
Z.post(ifrmId, C);
}
return D;
});
var y = "setField(key:" + b + ")";
Z.post(ifrmId, y);
Z.post(ifrmId, "setField(style:" + n.style + ")");
if (n.hasOwnProperty("customizeErrorRequired") && n.customizeErrorRequired == "true") {
Z.post(ifrmId, "customizeErrorRequired");
n.customizeErrorRequired = "false";
}
}
},
contains: function (x, z) {
for (var y = 0; y < x.length; y++) {
if (x[y] === z) {
return true;
}
}
return false;
},
renderWithErrorHandler: function (B, D, C, y, z, x, A) {
B.customizeErrorRequired = "true";
Z.render(B, D, C, z, x);
Z.customizeErrorHandler(y);
Z.removeCoverHandler(A);
},
runAfterRender: function (x) {
s = x;
},
render: function (C, B, H, A, I) {
console.log('testRender');
if (A != null && A != undefined) {
f = A;
}
if (I != null && I != undefined) {
v = I;
}
f = Number(f);
v = Number(v);
f = isNaN(f) ? 0 : f;
v = isNaN(v) ? 0 : v;
var F = j.length;
threedRedirected = false;
if (B && B.creditCardCountry && (B.creditCardCountry === "USA" || B.creditCardCountry === "CAN")) {
B.creditCardState = B.creditCardState || " ";
}
if (B) {
for (index = 0; index < F; index++) {
var D = "field_" + j[index];
if (B.hasOwnProperty(j[index])) {
C[D] = B[j[index]];
}
}
}
var J = Z.init(C, H);
if (!J) {
return;
}
if (B) {
var F = Object.keys(B).length;
o = p(B);
for (index = 0; index < F; index++) {
var z = Object.keys(B)[index];
if (Z.contains(j, z)) {
delete o[z];
}
}
} else {
o = null;
}
var y = document.getElementById("zuora_payment");
if (typeof y == "undefined" || !y) {
return { error: "invalid_request", error_description: "The container you specified does not exist" };
}
y.innerHTML = "";
Z.cleanUp(y, "z-overlay");
Z.cleanUp(y, "z-container");
if (n.style == "inline") {
Z.addInlineStyles();
Z.createIframe(y);
return;
}
if (n.style == "overlay") {
Z.addOverlayStyles();
var x = Z.generateDiv("z-overlay", "z-overlay");
y.appendChild(x);
var E = Z.generateDiv("z-container", "z-container");
y.appendChild(E);
var G = Z.generateDiv("z-data", "z-data");
G.tabindex = "-1";
E.appendChild(G);
Z.createIframe(document.getElementById("z-data"));
Z.activateOverlay("z-overlay");
}
},
cleanUp: function (y, x) {
var z = document.getElementById(x);
if (z != null) {
y.removeChild(z);
}
},
activateOverlay: function (y) {
try {
document.getElementById(y).style.display = "inline";
} catch (x) {}
},
deactivateOverlay: function (y) {
try {
document.getElementById(y).style.display = "none";
} catch (x) {}
},
generateDiv: function (A, y, x) {
var z = document.createElement("div");
z.id = A;
z.className = y;
z.border = "0";
if (z.addEventListener) {
z.addEventListener("click", x, false);
} else {
z.attachEvent("click", x);
}
return z;
},
addOverlayStyles: function () {
var B = document.createElement("style");
B.type = "text/css";
var y = document.createTextNode(i);
var C = document.createTextNode(u);
var x = document.createTextNode(h);
var A = document.createTextNode(l);
var z = document.createTextNode(t);
if (B.styleSheet) {
B.styleSheet.cssText = y.nodeValue + " " + C.nodeValue + " " + x.nodeValue + " " + z.nodeValue + " " + A.nodeValue;
} else {
B.appendChild(y);
B.appendChild(C);
B.appendChild(x);
B.appendChild(A);
B.appendChild(z);
}
document.getElementsByTagName("head")[0].appendChild(B);
},
addInlineStyles: function () {
var y = document.createElement("style");
y.type = "text/css";
var x = document.createTextNode(l);
if (y.styleSheet) {
y.styleSheet.cssText = x.nodeValue;
} else {
y.appendChild(x);
}
document.getElementsByTagName("head")[0].appendChild(y);
},
createIframe: function (y) {
var A = Z.createIframeURL();
var z = document.createElement("iframe");
z.setAttribute("src", A);
z.setAttribute("id", ifrmId);
z.setAttribute("overflow", "visible");
z.setAttribute("scrolling", "no");
z.setAttribute("frameBorder", "0");
z.setAttribute("allowtransparency", "true");
z.setAttribute("class", "z_hppm_iframe");
z.setAttribute("width", "100%");
z.setAttribute("height", "0");
var x = false;
z.addEventListener(
"load",
function () {
Z.prepopulate(o);
k();
g();
function B() {
if (q(y)) {
Z.post(ifrmId, "resize");
if (a(m.onloadCallback)) {
m.onloadCallback();
}
} else {
window.requestAnimationFrame(B);
}
}
if (!x) {
window.requestAnimationFrame(B);
x = true;
}
return false;
},
false
);
if (typeof options != "undefined") {
if (typeof options.vertical != "undefined" && options.vertical) {
z.style.width = "100%";
z.style.height = "100%";
}
}
y.appendChild(z);
},
createIframeURL: function () {
var x = d;
return x.concat(c).concat("zlog_level=" + ZLOG.getLevel());
},
post: function (A, z) {
var x = document.getElementById(A);
var y = encodeURIComponent(document.location.href);
var B = x.src;
if (B.indexOf(y) <= -1) {
x.src = B + "#" + y;
}
ZXD.postMessage(z, B, x.contentWindow);
return false;
},
allowScroll: function (y) {
if (ifrmId) {
var x = document.getElementById(ifrmId);
if (x) {
x.setAttribute("scrolling", "yes");
threedRedirected = true;
}
}
},
receive: function (x) {
ZFB.resizeCaller(ifrmId, x.action, x.height, x.width, f, v);
},
validate: function (z) {
if (z == null || z == undefined) {
Z.closeWindow();
var y = "Validate function required.";
alert(y);
if (!Z.isIE()) {
console.log(y);
}
return false;
}
m.validate = z;
var x = "validate";
Z.post(ifrmId, x);
},
customizeErrorHandler: function (y) {
if (y == null || y == undefined) {
Z.closeWindow();
var x = "Customized error message function required.";
alert(x);
if (!Z.isIE()) {
console.log(x);
}
return false;
}
m.customizeErrorMessage = y;
},
removeCoverHandler: function (x) {
if (x == null || x == undefined) {
return false;
}
m.removeCover = x;
},
sendErrorMessageToHpm: function (A, z) {
var y = { action: "customizeErrorMessage", key: A, message: z };
var x = JSON.stringify(y);
Z.post(ifrmId, x);
},
closeWindow: function () {
Z.deactivateOverlay("z-overlay");
Z.deactivateOverlay("z-container");
},
submit: function () {
var x = document.getElementById(ifrmId).src + "#" + encodeURIComponent(document.location.href);
document.getElementById(ifrmId).src = x;
ZXD.postMessage("postPage", x, document.getElementById(ifrmId).contentWindow);
return true;
},
responseHandler: function (x) {
var y = x.redirectUrl;
if (x.success) {
var z = y + "?refId=" + x.refId + "&success=" + x.success + "&signature=" + x.signature + "&token=" + x.token;
window.location.replace(z);
} else {
var z = y + "?errorCode=" + x.errorCode + "&errorMessage=" + x.errorMessage + "&success=" + x.success + "&signature=" + x.signature + "&token=" + x.token;
window.location.replace(z);
}
},
setEventHandler: function (x, y) {
if (x && y) {
m[x] = y;
}
},
setAgreement: function (A, B, z, y) {
if (!Z.validateAgreement(A, B, z)) {
return false;
}
var x = "mitConsentAgreementSrc";
Z.setFieldValue(x, A);
x = "mitCredentialProfileType";
Z.setFieldValue(x, B);
x = "agreementSupportedBrands";
Z.setFieldValue(x, z);
x = "mitConsentAgreementRef";
Z.setFieldValue(x, y);
return true;
},
setFieldValue: function (x, z) {
var y = "setField(" + x + ":" + z + ")";
Z.post(ifrmId, y);
},
validateAgreement: function (y, z, x) {
if (Z.isEmpty(y) && Z.isEmpty(z) && Z.isEmpty(x)) {
return true;
}
if (Z.isEmpty(y)) {
Z.alertRequired("mitConsentAgreementSrc");
return false;
}
if (Z.isEmpty(z)) {
Z.alertRequired("mitProfileType");
return false;
}
if (Z.isEmpty(x)) {
Z.alertRequired("agreementSupportedBrands");
return false;
}
if (y != "External") {
Z.alertIllegal("mitConsentAgreementSrc");
return false;
}
if (z != "Recurring" && z != "Unscheduled") {
Z.alertIllegal("mitProfileType");
return false;
}
return true;
},
isEmpty: function (x) {
return !x || 0 === x.length;
},
alertRequired: function (y) {
var x = "Param mitConsentAgreementSrc, mitProfileType and agreementSupportedBrands for Z.setAgreement should be all empty or not empty. Param " + y + " is empty.";
alert(x);
},
alertIllegal: function (y) {
var x = "Param " + y + " for Z.setAgreement is illegal.";
alert(x);
},
};
})();
export {Z};
I'm trying to export the render function to use in other js that is in the same folder but is not working.
I tried: put export at the beginning of the function (photo) and I also tried
export {render} from './zouralibrary';
but in thiscase is thorow me: Unexpected compilation error: 'default' is not exported by zouralibrary.js, imported by paymentIframeTest.js (3:7)

Refresh Code when Infinite Scroll Is Called

How to I call a JavaScript function when Tumblr's infinite scroll loads more posts?
I figure it would have to be some sort of listener function for when tumblrAutoPager.init us called. I found the infinite scroll code online and don't really understand it.
var tumblrAutoPager = {
url: "http://proto.jp/",
ver: "0.1.7",
rF: true,
gP: {},
pp: null,
ppId: "",
LN: location.hostname,
init: function () {
if ($("autopagerize_icon") || navigator.userAgent.indexOf('iPhone') != -1) return;
var tAP = tumblrAutoPager;
var p = 1;
var lh = location.href;
var lhp = lh.lastIndexOf("/page/");
var lht = lh.lastIndexOf("/tagged/");
if (lhp != -1) {
p = parseInt(lh.slice(lhp + 6));
tAP.LN = lh.slice(7, lhp);
} else if (lht != -1) {
tAP.LN = lh.slice(7);
if (tAP.LN.slice(tAP.LN.length - 1) == "/") tAP.LN = tAP.LN.slice(0, tAP.LN.length - 1);
} else if ("http://" + tAP.LN + "/" != lh) {
return;
};
var gPFncs = [];
gPFncs[0] = function (aE) {
var r = [];
for (var i = 0, l = aE.length; i < l; i++) {
if (aE[i].className == "autopagerize_page_element") {
r = gCE(aE[i]);
break;
}
}
return r;
};
gPFncs[1] = function (aE) {
var r = [];
for (var i = 0, l = aE.length; i < l; i++) {
var arr = aE[i].className ? aE[i].className.split(" ") : null;
if (arr) {
for (var j = 0; j < arr.length; j++) {
arr[j] == "post" ? r.push(aE[i]) : null;
}
}
}
return r;
};
gPFncs[2] = function (aE) {
var r = [];
var tmpId = tAP.ppId ? [tAP.ppId] : ["posts", "main", "container", "content", "apDiv2", "wrapper", "projects"];
for (var i = 0, l = aE.length; i < l; i++) {
for (var j = 0; j < tmpId.length; j++) {
if (aE[i].id == tmpId[j]) {
r = gCE(aE[i]);
tAP.ppId = aE[i].id;
break;
}
}
}
return r;
};
for (var i = 0; i < gPFncs.length; i++) {
var getElems = gPFncs[i](document.body.getElementsByTagName('*'));
if (getElems.length) {
tAP.gP = gPFncs[i];
tAP.pp = getElems[0].parentNode;
break;
}
}
function gCE(pElem) {
var r = [];
for (var i = 0, l = pElem.childNodes.length; i < l; i++) {
r.push(pElem.childNodes.item(i))
}
return r;
}
if (!tAP.pp) {
return;
}
sendRequest.README = {
license: 'Public Domain',
url: 'http://jsgt.org/lib/ajax/ref.htm',
version: 0.516,
author: 'Toshiro Takahashi'
};
function chkAjaBrowser() {
var A, B = navigator.userAgent;
this.bw = {
safari: ((A = B.split('AppleWebKit/')[1]) ? A.split('(')[0].split('.')[0] : 0) >= 124,
konqueror: ((A = B.split('Konqueror/')[1]) ? A.split(';')[0] : 0) >= 3.3,
mozes: ((A = B.split('Gecko/')[1]) ? A.split(' ')[0] : 0) >= 20011128,
opera: ( !! window.opera) && ((typeof XMLHttpRequest) == 'function'),
msie: ( !! window.ActiveXObject) ? ( !! createHttpRequest()) : false
};
return (this.bw.safari || this.bw.konqueror || this.bw.mozes || this.bw.opera || this.bw.msie)
}
function createHttpRequest() {
if (window.XMLHttpRequest) {
return new XMLHttpRequest()
} else {
if (window.ActiveXObject) {
try {
return new ActiveXObject('Msxml2.XMLHTTP')
} catch (B) {
try {
return new ActiveXObject('Microsoft.XMLHTTP')
} catch (A) {
return null
}
}
} else {
return null
}
}
};
function sendRequest(E, R, C, D, F, G, S, A) {
var Q = C.toUpperCase() == 'GET',
H = createHttpRequest();
if (H == null) {
return null
}
if ((G) ? G : false) {
D += ((D.indexOf('?') == -1) ? '?' : '&') + 't=' + (new Date()).getTime()
}
var P = new chkAjaBrowser(),
L = P.bw.opera,
I = P.bw.safari,
N = P.bw.konqueror,
M = P.bw.mozes;
if (typeof E == 'object') {
var J = E.onload;
var O = E.onbeforsetheader
} else {
var J = E;
var O = null
}
if (L || I || M) {
H.onload = function () {
J(H);
H.abort()
}
} else {
H.onreadystatechange = function () {
if (H.readyState == 4) {
J(H);
H.abort()
}
}
}
R = K(R, D);
if (Q) {
D += ((D.indexOf('?') == -1) ? '?' : (R == '') ? '' : '&') + R
}
H.open(C, D, F, S, A);
if ( !! O) {
O(H)
}
B(H);
H.send(R);
function B(T) {
if (!L || typeof T.setRequestHeader == 'function') {
T.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8')
}
return T
}
function K(X, V) {
var Z = [];
if (typeof X == 'object') {
for (var W in X) {
Y(W, X[W])
}
} else {
if (typeof X == 'string') {
if (X == '') {
return ''
}
if (X.charAt(0) == '&') {
X = X.substring(1, X.length)
}
var T = X.split('&');
for (var W = 0; W < T.length; W++) {
var U = T[W].split('=');
Y(U[0], U[1])
}
}
}
function Y(b, a) {
Z.push(encodeURIComponent(b) + '=' + encodeURIComponent(a))
}
return Z.join('&')
}
return H
}
function addNextPage(oj) {
if (oj.status == 404) {
tAP.remainFlg = false;
return;
}
var d = document.createElement("div");
d.innerHTML = oj.responseText;
var posts = tAP.gP(d.getElementsByTagName("*"));
if (posts.length < 2) {
tAP.rF = false;
return;
}
d = document.createElement("div");
d.className = "tumblrAutoPager_page_info";
tAP.pp.appendChild(d);
for (var i = 0; i < posts.length; i++) {
tAP.pp.appendChild(posts[i]);
}
var footer = $("footer");
footer ? footer.parentNode.appendChild(footer) : null;
tAP.rF = true;
}
watch_scroll();
function watch_scroll() {
var d = document.compatMode == "BackCompat" ? document.body : document.documentElement;
var r = d.scrollHeight - d.clientHeight - (d.scrollTop || document.body.scrollTop);
if (r < d.clientHeight * 2 && tAP.rF) {
tAP.rF = false;
p++;
sendRequest(addNextPage, "", "GET", "http://" + tAP.LN + "/page/" + p, true);
}
setTimeout(arguments.callee, 200);
};
function $(id) {
return document.getElementById(id)
};
},
switchAutoPage: function () {
this.rF = !this.rF;
var aE = document.getElementsByTagName('*');
for (var i = 0, l = aE.length; i < l; i++) {
if (aE[i].className == "tAP_switch") {
aE[i].firstChild.nodeValue = this.rF ? "AutoPage[OFF]" : "AutoPage[ON]";
}
}
}
};
window.addEventListener ? window.addEventListener('load', tumblrAutoPager.init, false) : window.attachEvent ? window.attachEvent("onload", tumblrAutoPager.init) : window.onload = tumblrAutoPager.init;

Cross date picker default date issue

hello all i am using the following js code to convert the input type date into three different text boxes for day,month and year.
js
(function () {
var sign = function(x) {
return typeof x === 'number' ? x ? x < 0 ? -1 : 1 : x === x ? 0 : NaN : NaN;
};
// TODO Calcular año bisiesto
var bisiesto = function(year)
{
return true;
// return (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) ? 1 : 0;
};
$.fn.insertAt = $.fn.insertAt || function(index, $parent) {
return this.each(function() {
if (index === 0) {
$parent.prepend(this);
} else {
$parent.children().eq(index - 1).after(this);
}
});
};
var Crossdp = function(e, o) {
if (e.data("cross-datepicker")) {
return this;
}
e.attr("type", "text");
o = $.extend({}, $.fn.cdp.defaults, o);
if(o.hideInput)
e.hide();
var cnt = $("<div>").addClass(o.classes.container || "").data("input", e).insertBefore(e);
// Data
var days = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
// Read format
var d = $("<select>").addClass(o.classes.controls || "").addClass(o.classes.days || ""),
m = $("<select>").addClass(o.classes.controls || "").addClass(o.classes.months || ""),
y = $("<select>").addClass(o.classes.controls || "").addClass(o.classes.year || "");
/**
* Gets the format metadata.
*/
var getFormat = function(format) {
var f = {},
last = "",
order = 0,
elements = {
"d": d,
"m": m,
"y": y
};
for(var i = 0, of=format; i < of.length; i++) {
var c = of[i];
if(last == c) {
f[c].count ++;
}
else if(c == "d" || c == "y" || c == "m") {
f[c] = {
"count": 1,
"order": order++,
"e": elements[c]
};
elements[c].data("order", f[c].order);
last = c;
}
if(order > 3) {
throw "Invalid date format";
}
}
return f;
};
var iF = getFormat(o.inputFormat),
f = getFormat(o.format);
for(var i in f) {
f[i].e.appendTo(cnt);
}
cnt.sort(function(a, b) {
if(a.data("order") > b.data("order")) {
return 1;
}
else if(a.data("order") < b.data("order")) {
return -1;
}
else {
return 0;
}
});
// Helpers
/**
* Format a numeric day to string.
*/
var formatDay = function(day, format) {
var text = String(day),
c = format || f.d.count;
while(c > text.length) {
text = "0" + text;
}
return text;
};
/**
* Format a numeric month to string.
*/
var formatMonth = function(month, format) {
if(month > 12) {
throw "Invalid month: "+month;
}
var c = format || f.m.count,
text = String(month);
if(c == 2) {
if(text.length == 1) {
text = "0" + text;
}
}
else if(c == 3) {
text = o.months[i-1].substr(0, 3);
}
else if(c == 4) {
text = o.months[i-1];
}
else {
throw "Invalid month format";
}
return text;
};
/**
* Format a numeric month to string.
*/
var formatYear = function(year, format) {
var text = String(year),
c = format || f.y.count;
if(c == 2) {
text = text.substr(text.length-2, 2);
}
else if(c != 4) {
throw "Invalid year format";
}
return text;
};
var parseYear = function(date, format) {
// TODO
};
// Update input function
var formatDate = function(resultFormat, readFormat, years, months, days) {
var a = ["d", "m", "y"],
result = resultFormat;
if(typeof days === 'string')
days = parseInt(days);
if(typeof months === 'string')
months = parseInt(months);
if(typeof years === 'string')
years = parseInt(years);
for(var i = 0; i < a.length; i++) {
var ch = a[i], /* Example: a[0]='d' */
format = readFormat[ch], /* Example: uF['d']='dd' */
word = "",
formatted = "";
for(var j = 0; j < format.count; j++) {
word += ch;
}
if(ch == "d") {
formatted = formatDay(days, format.count);
}
else if(ch == "m") {
formatted = formatMonth(months, format.count);
}
else {
formatted = formatYear(years, format.count);
}
result = result.replace(word, formatted);
}
return result;
};
var updateInput = function() {
e.val(formatDate(o.inputFormat, iF, y.val(), m.val(), d.val()));
};
this.updateInput = function() {
updateInput();
};
var updateFromInput = function() {
// TODO
};
// Generate 3 selects
/* Days */
d.data("days", 0);
/**
* Days of determinated month.
*/
var generateDays = function(month) {
if(d.data("days") == days[month-1]) {
return;
}
var selected = parseInt(d.val() || "1");
d.html("");
if(month == 0) {
return;
}
if(o.addNullOption) {
d.append("<option value=''>"+o.nullOptionText+"</option>");
}
for(var i = 1; i <= days[month-1]; i++) {
$("<option>").attr("value", i).text(formatDay(i)).appendTo(d);
}
d.val(selected);
};
d.change(function() {
updateInput();
});
generateDays(1);
/* Months */
m.change(function() {
// Regenerate days
generateDays(parseInt($(this).val()));
updateInput();
});
if(o.addNullOption) {
m.append("<option value='0'>"+o.nullOptionText+"</option>");
}
for(var i = 1; i <= 12; i++) {
m.append("<option value='"+i+"'>"+formatMonth(i)+"</option>");
}
/* Years */
var from,
to;
if(typeof o.years[0] == 'string') {
var current = new Date().getFullYear(),
count;
if(o.years.length == 3) {
current += o.years[1];
count = o.years[2];
}
else {
count = o.years[1];
}
for(var i = current; i != current + count; i += sign(count)) {
y.append("<option value='"+i+"'>"+formatYear(i)+"</option>");
}
}
else {
for(var i = o.years[0]; i != o.years[1]; i += sign(o.years[1]-o.years[0])) {
y.append("<option value='"+i+"'>"+formatYear(i)+"</option>");
}
}
y.change(function() {
updateInput();
});
// Save
this.inputs = {
d: d,
y: y,
m: m
};
// Finish
if(e.data("initial-day")) {
$(function() {
$.fn.cdp.statics.fns.set(e, [
e.data("initial-year"),
e.data("initial-month"),
e.data("initial-day")]);
});
}
updateInput();
e.data("cross-datepicker", this);
};
$.fn.cdp = function (o, arg) {
var e = $(this);
if (e.length == 0) {
return this;
}
else if (e.length > 1) {
e.each(function () {
$(this).cdp(o);
});
return this;
}
if(!e.is("input")) {
throw "You can apply Cross-DatePicker only on an 'input' element";
}
if(typeof o === 'string') {
var st = $.fn.cdp.statics;
if(!st.fns[o]) {
console.error("Unknown function "+o);
}
st.fns[o](e, arg);
return this;
}
var cdp = new Crossdp(e, o);
return this;
}
$.fn.cdp.defaults = {
hideInput: true,
format: "d/mmm/yyyy",
inputFormat: "yyyy-mm-dd",
years: ["now", -100], // [initial year, final year] or ["now", relative years count] or ["now", relative years from, relative years count]
months: ["Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre"],
addNullOption: false,
nullOptionText: "Select",
classes: {
container: "cdp-container",
controls: "cdp-select",
days: "cdp-d",
months: "cdp-m",
years: "cdp-y"
}
};
$.fn.cdp.statics = {
fns: {
set: function(e, arg) {
var st = $.fn.cdp.statics,
obj = e.data("cross-datepicker"),
y,m,d;
if($.isArray(arg)) {
y = arg[0];
m = arg[1];
d = arg[2];
}
else if(typeof arg === 'string') {
var array = arg.split("-");
y = parseInt(array[0]);
m = parseInt(array[1]);
d = parseInt(array[2]);
}
else {
y = arg.year || arg.y;
m = arg.month || arg.m;
d = arg.day || arg.d;
}
obj.inputs.y.val(String(y));
obj.inputs.m.val(String(m));
obj.inputs.d.val(String(d));
obj.updateInput();
}
}
};
})();
how to implement
html
<input type="date" data-initial-day="20" data-initial-year="2010" data-initial-month="64" />
<!-- Required scripts -->
<script src='https://code.jquery.com/jquery-2.1.0.min.js'></script>
<script src='../src/cross-datepicker.js'></script>
<script>
$(function() {
$("input[type='date']").cdp();
});
</script>
the problem is that the code works great when defined data-initial-year for month and day also but when i want to show the default none selected date like select day select month and select year it shows blank or 0 .
i dont know how to solve this i have tried to solve the problem by adding some text into the js file but no help .
if you can suggest me something it would be great.
The plugin code needs to check if an entry is valid before setting it.
First off, set the addNullOption to true so that a "Select" text is visible.
$(function() {
$("input[type='date']").cdp({
addNullOption : true,
nullOptionText: "Select"
});
});
Then modify the last function $.fn.cdp.statics with the "check valid" code included below (demo):
$.fn.cdp.statics = {
fns: {
set: function(e, arg) {
var st = $.fn.cdp.statics,
obj = e.data("cross-datepicker"),
y,m,d;
if($.isArray(arg)) {
y = arg[0];
m = arg[1];
d = arg[2];
}
else if(typeof arg === 'string') {
var array = arg.split("-");
y = parseInt(array[0]);
m = parseInt(array[1]);
d = parseInt(array[2]);
}
else {
y = arg.year || arg.y;
m = arg.month || arg.m;
d = arg.day || arg.d;
}
// check valid
if ( obj.inputs.y.find('option[value="' + y + '"]').length ) {
obj.inputs.y.val(String(y));
}
if ( obj.inputs.m.find('option[value="' + m + '"]').length ) {
obj.inputs.m.val(String(m));
}
if ( obj.inputs.d.find('option[value="' + d + '"]').length ) {
obj.inputs.d.val(String(d));
}
obj.updateInput();
}
}
};
To show "Select" in the other selects, I ended up adding the following code because the year select didn't have a null option:
if (o.addNullOption) {
y.append("<option value='0'>"+o.nullOptionText+"</option>");
}
and changing the "check valid" code to:
// check valid
y = obj.inputs.y.find('option[value="' + y + '"]').length ? y : 0;
obj.inputs.y.val(String(y));
m = obj.inputs.m.find('option[value="' + m + '"]').length ? m : 0;
obj.inputs.m.val(String(m));
d = obj.inputs.d.find('option[value="' + d + '"]').length ? d : 0;
obj.inputs.d.val(String(d));
Get the full code from this updated demo.

Javascript error: Cannot read property 'parentNode' of null

The javascript I am using:
javascript: c = '{unit}, ({coords}) {player} |{distance}| {return}';
p = ['Scout', 'LC', 'HC', 'Axe', 'Sword', 'Ram', '***Noble***'];
function V() {
return 1;
}
window.onerror = V;
function Z() {
d = (window.frames.length > 0) ? window.main.document : document;
aid = d.getElementById('editInput').parentNode.innerHTML.match(/id\=(\d+)/)[1];
function J(e) {
vv = e.match(/\d+\|\d+/g);
return (vv ? vv[vv.length - 1].match(/((\d+)\|(\d+))/) : null);
}
function K(e) {
f = parseInt(e, 10);
return (f > 9 ? f : '0' + f);
}
function L(g, e) {
return g.getElementsByTagName(e);
}
function N(g) {
return g.innerHTML;
}
function M(g) {
return N(L(g, 'a')[0]);
}
function O() {
return k.insertRow(E++);
}
function W(f) {
return B.insertCell(f);
}
function P(g, e) {
g.innerHTML = e;
return g;
}
function X(e) {
C = B.appendChild(d.createElement('th'));
return P(C, e);
}
function Y(f) {
return K(f / U) + ':' + K(f % (U) / T) + ':' + K(f % T);
}
U = 3600;
T = 60;
R = 'table';
S = 'width';
s = L(document, R);
for (j = 0; j < s.length; j++) {
s[j].removeAttribute(S);
if (s[j].className == 'main') {
s = L(L(s[j], 'tbody')[0], R);
break;
}
}
D = 0;
for (j = 0; j < s.length; j++) {
s[j].removeAttribute(S);
if (s[j].className = 'vis') {
k = s[j];
if (t = k.rows) {
D = t.length;
break;
}
}
}
for (E = 0; E < D; E++) {
l = t[E];
m = (u = l.cells) ? u.length : 0;
if (m) {
u[m - 1].colSpan = 5 - m;
if (N(u[0]) == 'Arrival:') {
Q = new Date(N(u[1]).replace(/<.*/i, ''));
} else {
if (N(u[0]) == 'Arrival in:') {
v = N(u[1]).match(/\d+/ig);
}
}
if (E == 1) {
G = M(u[2]);
}
if (E == 2) {
w = J(M(u[1]));
}
if (E == 4) {
x = J(M(u[1]));
}
}
}
y = v[0] * U + v[1] * T + v[2] * 1;
n = w[2] - x[2];
o = w[3] - x[3];
F = Math.sqrt(n * n + o * o);
H = F.toFixed(2);
E = D - 2;
s = L(k, 'input');
i = s[1];
h = s[0];
h.size = T;
B = O();
P(W(0), 'Distance:').colSpan = 2;
P(W(1), H + ' Fields').colSpan = 2;
B = O();
X('Unit');
X('Sent');
X('Duration');
X('Name to');
c = c.replace(/\{coords\}/i, w[1]).replace(/\{distance\}/i, H).replace(/\{player\}/i, G);
for (j in p) {
z = Math.round([9.00000000, 10.00000000, 11.00000000, 18.0000000015, 22.00000000, 30.00000000, 35.0000000][j] * T * F);
A = z - y;
if (A > 0) {
I = Y(z);
B = O();
P(W(0), p[j]);
P(W(1), A < T && 'just now' || A < U && Math.floor(A / T) + ' mins ago' || Y(A) + ' ago');
P(W(2), I);
C = W(3);
q = C.appendChild(i.cloneNode(1));
r = C.appendChild(h.cloneNode(1));
r.id = 'I' + j;
r.value = c.replace(/\{duration\}/i, I).replace(/\{sent\}/i, new Date(Q.valueOf() - z * 1000).toLocaleString().replace(/.\d{4}/i, '').replace(/(\w{3})\w*/i, '$1')).replace(/\{return\}/i, new Date(Q.valueOf() + z * 1000).toString().replace(/\w+\s*/i, '').replace(/(\d*:\d*:\d*)(.*)/i, '$1')).replace(/\{unit\}/i, p[j]).replace(/\{attack_id\}/i, aid);
q.onmousedown = new Function('h.value=d.getElementById(\'I' + j + '\').value;');
}
}
}
Z();
The error I receive:
Uncaught TypeError: Cannot read property 'parentNode' of null
A URL looks like this:
game.php?village=2100&id=4348754&type=other&screen=info_command
There are two possibilities:
editInput is a typo, and the actual id of that element is different (ids are case-sensitive).
You are executing this code while the DOM is not ready. To prevent this, execute the code just before the </body> closing tag, or wrap it in an event handler for the load event of window or the DOMContentLoaded event of document.
EDITED How to wrap your code:
window.onload = function() {
//your code here
};
In my case it was a conflict between slider and form validator as they were located on different pages but javascript file would of target both of them simultaneously, so all pages that had no slider would come up with "Cannot read property 'parentNode' of null" error, so I have added to the slider If statement:
if(document.getElementById("mainImage")){
var myImage = document.getElementById("mainImage");
var linkElement = myImage.parentNode;
that solved my problem, try it
I was trying to remove child element from parent:
constructor(private renderer: Renderer2) { }
removeChildEl() {
this.renderer.removeChild(parent, child);
}
but for some reason parent-element does not exist so I need to check via if condition:
removeChildEl() {
if(parent)
this.renderer.removeChild(parent, child);
}
so if the parent exists then remove the child otherwise do nothing.

Categories