I'm working on the option for hiding the panel's bodies and keep them hidden by reloading the page. It's a bootstrap driven code and the operation $("#requests").collapse() produces an animation that I don't like to see cause having 100 of panels collapsing each time by reloading the page can be annoying. So is there some better method to do this?
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i = 0; i <ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length,c.length);
}
}
return "";
}
function setCookie(cname) {
var d = new Date();
d.setTime(d.getTime() + (365*24*60*60*1000));
var expires = "expires="+d.toUTCString();
if(getCookie(cname) == 1) {
document.cookie = cname + "=" + 0 + "; " + expires;
} else {
document.cookie = cname + "=" + 1 + "; " + expires;
}
}
window.onload = function applyCookies() {
if(getCookie("hidden") == 1) {
$("#requests").collapse();
}
}
if(getCookie("hidden") == 1) {
$("#requests").hide();
}
Add #request{
display:none;
}
Now, you just change your function to this
window.onload = function applyCookies() {
if(getCookie("hidden") == 0) {
$("#requests").css("display","block");
}
}
I have a website which supports two languages. In Chrome switching languages works great, but in Firefox it doesn't matter which button I click, for "english" or "german" language it always set my language variable for german language which is set by default
Can someone help me to resolve this issue?
here is example function where I call getLangCookie function
var lang = getLangCookie('lang');
console.log('lang = ', lang);
$.ajax({
type: "GET",
url: '/menu.xml',
dataType: "xml",
success: function (xml) {
$(xml).find('description ' + lang).each(function () {
$(this).parent().html($(this).html());
}
);
var menu = [];
var data = $.xml2json(xml)['#document'];
that.menu = data.menu;
console.log('menu = ', that.menu);
}
}
);
function getLangCookie(lang) {
var name = lang + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') c = c.substring(1);
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
// by clicking on English button set the cookie value
function onEnglishbtn() {
setLangCookie("lang", "en", 30);
document.location.reload();
var lang = getLangCookie('lang');
return lang;
}
function setLangCookie(lang, value, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
var expires = "expires=" + d.toGMTString();
document.cookie = lang + "=" + value + "; " + expires;
}
// function onload from index.html setting up the lang by default
window.onload = function () {
setLangCookie("lang", "de", 30);
if (typeof window.localStorage !== "undefined" && !localStorage.getItem('visited')) {
localStorage.setItem('visited', true);
setLangCookie("lang", "de", 30);
}
}
I think you should put the ajax request to the onload function as well something like this.
function getLangCookie(lang) {
var name = lang + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') c = c.substring(1);
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
// by clicking on English button set the cookie value
function onEnglishbtn() {
setLangCookie("lang", "en", 30);
document.location.reload();
var lang = getLangCookie('lang');
return lang;
}
function setLangCookie(lang, value, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
var expires = "expires=" + d.toGMTString();
document.cookie = lang + "=" + value + "; " + expires;
}
// function onload from index.html setting up the lang by default
window.onload = function () {
var lang_cookie = getLangCookie('lang');
console.log('lang_cookie = ', lang_cookie);
// if cookie doesn't exist
if (lang_cookie !== null) {
setLangCookie("lang", "de", 30);
} // if cookie exists
else {
console.log('lang_cookie exists!');
setLangCookie("lang", lang_cookie, 30);
}
$.ajax({
type: "GET",
url: '/menu.xml',
dataType: "xml",
success: function (xml) {
$(xml).find('description ' + lang_cookie).each(function () {
$(this).parent().html($(this).html());
}
);
var menu = [];
var data = $.xml2json(xml)['#document'];
that.menu = data.menu;
console.log('menu = ', that.menu);
}
});
}
I would like to setup a file download page similar to the one here on Oracle's website.
How do I recreate this?
I'm not familiar with writing javascript, but I see that the are using javascript to display the table and control the popup and also replace the link once the "Agree to Terms" radio button has been selected. They're using this code:
// Dynamically generated download page for OTN.
// Aurelio Garcia-Ribeyro, 2012-05-21, based off of pre-existing code for OTN license acceptance
// Only the 'downloads' array should need to be modified, unless the look/feel needs to be changed in the body at the bottom
// 'downloads' is an array of releases, using the CDS part number as the key for each release. Each release is also an array of meta-data for that release. Finally, there is a 3rd nested array of files to be offered in the release.
// downloads[product][path] will assumed to be the path for each file. NOT YET IMPLEMENTED: Unless downloads[product]['files']['path'] is defined.
// The filename will assumed to be the key for path from downloads[product]['files']. NOT YET IMPLEMENTED: Unless downloads[product]['files']['filename'] is defined.
var page_title = "Java SE Development Kit 8 Downloads";
var page_blurb = '<p>Thank you for downloading this release of the Java<sup>TM</sup> Platform, Standard Edition Development Kit (JDK<sup>TM</sup>). The JDK is a development environment for building applications, applets, and components using the Java programming language.</p> <p>The JDK includes tools useful for developing and testing programs written in the Java programming language and running on the Java platform.<br></p><div>See also:</div> <div> <div> <ul> <li>Java Developer Newsletter (tick the checkbox under Subscription Center > Oracle Technology News)</li> <li>Java Developer Day hands-on workshops (free) and other events</li> <li>Java Magazine</li> </ul> JDK MD5 Checksum </div> <br> <b>Looking for JDK 8 on ARM?</b> <br>JDK 8 for ARM downloads have moved to the JDK 8 for ARM download page.</div>';
var downloads = new Array();
downloads['jdk-8u31-oth-JPR'] = new Array();
downloads['jdk-8u31-oth-JPR']['title'] = "Java SE Development Kit 8u31";
downloads['jdk-8u31-oth-JPR']['lictitle'] = "Oracle Binary Code License Agreement for Java SE";
downloads['jdk-8u31-oth-JPR']['licpath'] = "/technetwork/java/javase/terms/license";
downloads['jdk-8u31-oth-JPR']['files'] = new Array();
downloads['jdk-8u31-oth-JPR']['files']['jdk-8u31-linux-i586.rpm'] = { "title":"Linux x86", "size":"135.24 MB","filepath":"http://download.oracle.com/otn-pub/java/jdk/8u31-b13/jdk-8u31-linux-i586.rpm"};
downloads['jdk-8u31-oth-JPR']['files']['jdk-8u31-linux-i586.tar.gz'] = { "title":"Linux x86", "size":"154.91 MB","filepath":"http://download.oracle.com/otn-pub/java/jdk/8u31-b13/jdk-8u31-linux-i586.tar.gz"};
downloads['jdk-8u31-oth-JPR']['files']['jdk-8u31-linux-x64.rpm'] = { "title":"Linux x64", "size":"135.62 MB","filepath":"http://download.oracle.com/otn-pub/java/jdk/8u31-b13/jdk-8u31-linux-x64.rpm"};
downloads['jdk-8u31-oth-JPR']['files']['jdk-8u31-linux-x64.tar.gz'] = { "title":"Linux x64", "size":"153.45 MB","filepath":"http://download.oracle.com/otn-pub/java/jdk/8u31-b13/jdk-8u31-linux-x64.tar.gz"};
downloads['jdk-8u31-oth-JPR']['files']['jdk-8u31-macosx-x64.dmg'] = { "title":"Mac OS X x64", "size":"209.17 MB","filepath":"http://download.oracle.com/otn-pub/java/jdk/8u31-b13/jdk-8u31-macosx-x64.dmg"};
downloads['jdk-8u31-oth-JPR']['files']['jdk-8u31-solaris-sparcv9.tar.Z'] = { "title":"Solaris SPARC 64-bit (SVR4 package)", "size":"136.91 MB","filepath":"http://download.oracle.com/otn-pub/java/jdk/8u31-b13/jdk-8u31-solaris-sparcv9.tar.Z"};
downloads['jdk-8u31-oth-JPR']['files']['jdk-8u31-solaris-sparcv9.tar.gz'] = { "title":"Solaris SPARC 64-bit", "size":"97.11 MB","filepath":"http://download.oracle.com/otn-pub/java/jdk/8u31-b13/jdk-8u31-solaris-sparcv9.tar.gz"};
downloads['jdk-8u31-oth-JPR']['files']['jdk-8u31-solaris-x64.tar.Z'] = { "title":"Solaris x64 (SVR4 package)", "size":"137.51 MB","filepath":"http://download.oracle.com/otn-pub/java/jdk/8u31-b13/jdk-8u31-solaris-x64.tar.Z"};
downloads['jdk-8u31-oth-JPR']['files']['jdk-8u31-solaris-x64.tar.gz'] = { "title":"Solaris x64", "size":"94.82 MB","filepath":"http://download.oracle.com/otn-pub/java/jdk/8u31-b13/jdk-8u31-solaris-x64.tar.gz"};
downloads['jdk-8u31-oth-JPR']['files']['jdk-8u31-windows-i586.exe'] = { "title":"Windows x86", "size":"157.96 MB","filepath":"http://download.oracle.com/otn-pub/java/jdk/8u31-b13/jdk-8u31-windows-i586.exe"};
downloads['jdk-8u31-oth-JPR']['files']['jdk-8u31-windows-x64.exe'] = { "title":"Windows x64", "size":"170.36 MB","filepath":"http://download.oracle.com/otn-pub/java/jdk/8u31-b13/jdk-8u31-windows-x64.exe"};
downloads['jdk-8u31-demos-oth-JPR'] = new Array();
downloads['jdk-8u31-demos-oth-JPR']['title'] = "Java SE Development Kit 8u31 Demos and Samples Downloads";
downloads['jdk-8u31-demos-oth-JPR']['lictitle'] = "Oracle BSD License.";
downloads['jdk-8u31-demos-oth-JPR']['licpath'] = "/technetwork/java/javase/terms/license/oraclebsd-1603217.txt";
//downloads['jdk-8u31-demos-oth-JPR']['licNoAccept'] = true;
downloads['jdk-8u31-demos-oth-JPR']['files'] = new Array();
downloads['jdk-8u31-demos-oth-JPR']['files']['jdk-8u31-linux-i586-demos.rpm'] = { "title":"Linux x86", "size":"58.65 MB","filepath":"http://download.oracle.com/otn-pub/java/jdk/8u31-b13-demos/jdk-8u31-linux-i586-demos.rpm"};
downloads['jdk-8u31-demos-oth-JPR']['files']['jdk-8u31-linux-i586-demos.tar.gz'] = { "title":"Linux x86", "size":"58.5 MB","filepath":"http://download.oracle.com/otn-pub/java/jdk/8u31-b13-demos/jdk-8u31-linux-i586-demos.tar.gz"};
downloads['jdk-8u31-demos-oth-JPR']['files']['jdk-8u31-linux-x64-demos.rpm'] = { "title":"Linux x64", "size":"58.71 MB","filepath":"http://download.oracle.com/otn-pub/java/jdk/8u31-b13-demos/jdk-8u31-linux-x64-demos.rpm"};
downloads['jdk-8u31-demos-oth-JPR']['files']['jdk-8u31-linux-x64-demos.tar.gz'] = { "title":"Linux x64", "size":"58.56 MB","filepath":"http://download.oracle.com/otn-pub/java/jdk/8u31-b13-demos/jdk-8u31-linux-x64-demos.tar.gz"};
downloads['jdk-8u31-demos-oth-JPR']['files']['jdk-8u31-macosx-x86_64-demos.zip'] = { "title":"Mac OS X", "size":"59.22 MB","filepath":"http://download.oracle.com/otn-pub/java/jdk/8u31-b13-demos/jdk-8u31-macosx-x86_64-demos.zip"};
downloads['jdk-8u31-demos-oth-JPR']['files']['jdk-8u31-solaris-sparcv9-demos.tar.Z'] = { "title":"Solaris SPARC 64-bit", "size":"13.51 MB","filepath":"http://download.oracle.com/otn-pub/java/jdk/8u31-b13-demos/jdk-8u31-solaris-sparcv9-demos.tar.Z"};
downloads['jdk-8u31-demos-oth-JPR']['files']['jdk-8u31-solaris-sparcv9-demos.tar.gz'] = { "title":"Solaris SPARC 64-bit", "size":"9.27 MB","filepath":"http://download.oracle.com/otn-pub/java/jdk/8u31-b13-demos/jdk-8u31-solaris-sparcv9-demos.tar.gz"};
downloads['jdk-8u31-demos-oth-JPR']['files']['jdk-8u31-solaris-x64-demos.tar.Z'] = { "title":"Solaris x64", "size":"13.52 MB","filepath":"http://download.oracle.com/otn-pub/java/jdk/8u31-b13-demos/jdk-8u31-solaris-x64-demos.tar.Z"};
downloads['jdk-8u31-demos-oth-JPR']['files']['jdk-8u31-solaris-x64-demos.tar.gz'] = { "title":"Solaris x64", "size":"9.25 MB","filepath":"http://download.oracle.com/otn-pub/java/jdk/8u31-b13-demos/jdk-8u31-solaris-x64-demos.tar.gz"};
downloads['jdk-8u31-demos-oth-JPR']['files']['jdk-8u31-windows-i586-demos.zip'] = { "title":"Windows x86", "size":"60.33 MB","filepath":"http://download.oracle.com/otn-pub/java/jdk/8u31-b13-demos/jdk-8u31-windows-i586-demos.zip"};
downloads['jdk-8u31-demos-oth-JPR']['files']['jdk-8u31-windows-x64-demos.zip'] = { "title":"Windows x64", "size":"60.43 MB","filepath":"http://download.oracle.com/otn-pub/java/jdk/8u31-b13-demos/jdk-8u31-windows-x64-demos.zip"};
function disableAnchor(obj, disable){
if(disable){
var href = obj.getAttribute("href");
if(href && href != "" && href != null){
obj.setAttribute('href_bak', href);
}
// obj.setAttribute('href', 'http://www.oracle.com/technetwork/licenses/sorry-150381.html');
obj.removeAttribute('href');
obj.setAttribute('class', 'boldbodylink');
} else {
obj.setAttribute('href', obj.getAttribute('href_bak'));
obj.setAttribute('class', 'boldbodylink');
}
}
function disableAnchorByName(anchorname, disable){
// var use_gebi=false;
var o=null;
// if (document.getElementById) { use_gebi=true; }
// Logic to find position
// if (use_gebi) {
// o=document.getElementById(anchorname);
// } else {
for (var i=0; i<document.anchors.length; i++) {
if (document.anchors[i].name==anchorname) { o=document.anchors[i]; break; }
}
disableAnchor(o, disable);
}
function disableAnchorByName(doc, anchorname, disable, enabledHref, onclickFtn){
var use_gebi=false;
var o=null;
for (var i=0; i<doc.anchors.length; i++) {
if (doc.anchors[i].name==anchorname) { o=doc.anchors[i]; break; }
}
disableAnchor(o, disable, enabledHref, onclickFtn);
}
function disableAnchor( obj, disable, enabledHref, onclickFtn ){
if(disable){
obj.onclick = onclickFtn;
// obj.setAttribute('onclick', disabledHref );
// obj.removeAttribute('href');
// obj.setAttribute('href', onclickFtn );
obj.setAttribute('class', 'boldbodylink');
} else {
obj.setAttribute('href', enabledHref );
obj.onclick = null;
obj.setAttribute('class', 'boldbodylink');
}
}
function disableDownloadAnchors(doc, disabled, part){
var agreementPrompt = new Function( "alert('Sorry, you must accept the License Agreement before downloading.');" );
if (downloads[part]['title']) {
for(var b in downloads[part]['files']) {
if (downloads[part]['files'][b]['title']) {
disableAnchorByName(doc, part + 'XXX' + b, disabled, downloads[part]['files'][b]['filepath'], agreementPrompt);
}
}
}
}
function youMustAgreePrompt(){
alert('Sorry, you must accept the License Agreement before downloading.');
}
function acceptAgreement(windowRef, part){
var doc = windowRef.document;
disableDownloadAnchors(doc, false, part);
hideAgreementDiv(doc, part);
writeSessionCookie( 'oraclelicense', 'accept-securebackup-cookie' );
}
function declineAgreement(windowRef, part){
var doc = windowRef.document;
disableDownloadAnchors(doc, true, part);
writeSessionCookie( 'oraclelicense', 'decline' );
// forward();
}
function showAgreement(part){
window.open(downloads[part]['licpath'],'LicenseAgreement','status=1,scrollbars=1,width=500,height=400,top=150,left=400');
}
function showAgreement2(part){
window.open(downloads[part]['licpath2'],'LicenseAgreement','status=1,scrollbars=1,width=500,height=400,top=150,left=400');
}
function forward(){
location.href="http://www.oracle.com/technetwork/licenses/sorry-150381.html";
}
function hideAgreementDiv(doc, part) {
if (doc.getElementById) { // DOM3 = IE5, NS6
doc.getElementById('agreementDiv' + part).style.visibility = 'hidden';
doc.getElementById('thankYouDiv' + part).style.visibility = 'visible';
} else {
if (doc.layers) { // Netscape 4 BROKEN?
doc.getElementById('agreementDiv' + part).visibility = 'hidden';
doc.getElementById('thankYouDiv' + part).visibility = 'visible';
} else { // IE 4 BROKEN?
doc.all.getElementById('agreementDiv' + part).style.visibility = 'hidden';
doc.all.getElementById('thankYouDiv' + part).style.visibility = 'visible';
}
}
}
function resetAgreementForm(){
//alert('here 1');
var cookie = getCookieValue('oraclelicense');
var myRadios = document.forms['agreementForm']['agreement'];
if(cookie == null){
document.forms['agreementForm'].reset();
} else if(cookie == 'accept-securebackup-cookie') {
myRadios[0].checked = 'true';
acceptAgreement(part);
} else if(cookie == 'decline'){
myRadios[1].checked = 'true';
}
}
document.write('<a name="top" id="top"></a><h1>' + page_title + '</h1><br />');
document.write(page_blurb + '<br/>');
// Add this section in if there are multiple products and you want an anchored table of contents at the top
// document.write('<UL>');
// for(var d in downloads) {
// if (downloads[d]['files']) {
// document.write('<li>' + downloads[d]['title'] + '</li>');
// }
// }
// document.write('</ul>');
for(var d in downloads) {
if (downloads[d]['files']) {
document.write('<br /><table width="100%" border="0" style="border-collapse: collapse; border: 1px solid rgb(204, 204, 204); margin: 1px;"><tbody>');
if (downloads[d]['title']) document.write('<tr bgcolor="#c0c0c0"><td colspan="3" cellspacing="0" cellpadding="1"><a id="' + d + '" name="' + d + '"></a><h3> ' + downloads[d]['title'] + '</h3></td></tr>');
if (downloads[d]['lictitle'])
if (downloads[d]['licNoAccept']) {
document.write('<tr bgcolor="#d0d0d0"><th colspan="3" cellspacing="0" cellpadding="1"><div class="lic_div">');
document.write('<BR><p>' + downloads[d]['title'] + ' are released under the ' + downloads[d]['lictitle'] + '</p>');
document.write('</div></th></tr>');
} else {
document.write('<tr bgcolor="#d0d0d0"><th colspan="3" cellspacing="0" cellpadding="1"><div class="lic_div">');
document.write('<p>You must accept the ' + downloads[d]['lictitle'] + '');
if (downloads[d]['lictitle2']) document.write(' and ' + downloads[d]['lictitle2'] + '');
document.write(' to download this software.</p>');
document.write('<div align="left" id="agreementDiv' + d + '"><form name="agreementForm' + d + '" method="post" action="radio" class="lic_form"><input type="radio" value="on" name="agreement' + d + '" onClick="acceptAgreement(window.self, \'' + d + '\');" /> Accept License Agreement <input type="radio" value="on" name="agreement' + d + '" onClick="declineAgreement(window.self, \'' + d + '\');" checked="checked" /> Decline License Agreement</form></div>');
document.write('<div style="visibility: hidden;" id="thankYouDiv' + d + '" class="com_msg">Thank you for accepting the ' + downloads[d]['lictitle'] + '; you may now download this software.<br/></div>');
document.write('<br /> </div></th></tr>');
}
document.write('<tr bgcolor="#e0e0e0"><th cellspacing="0" cellpadding="1"><h4> Product / File Description</h4></th><th><h4>File Size</h4></th><th><h4>Download</h4></th></tr>');
for(var e in downloads[d]['files']) {
if (downloads[d]['files'][e]['title']) {
if (downloads[d]['lictitle'] && !(downloads[d]['licNoAccept']))
document.write('<tr valign="top" style="border-bottom:1px solid #CCC; margin-bottom:1px;"><td> ' + downloads[d]['files'][e]['title'] + '</td><td align="right">' + downloads[d]['files'][e]['size'] + ' </td><td align="left"><a name="' + d + 'XXX' + e + '" href="javascript: void(0)" onclick="youMustAgreePrompt();" id="' + d + 'XXX' + e + '"> <img src="/ocom/groups/public/#otn/documents/digitalasset/115899.gif"> ' + e + '</a></td></tr>');
else
document.write('<tr valign="top" style="border-bottom:1px solid #CCC; margin-bottom:1px;"><td> ' + downloads[d]['files'][e]['title'] + '</td><td align="right">' + downloads[d]['files'][e]['size'] + ' </td><td align="left"><a name="' + d + 'XXX' + e + '" href="' + downloads[d]['files'][e]['filepath'] + '" id="' + d + 'XXX' + e + '"> <img src="/ocom/groups/public/#otn/documents/digitalasset/115899.gif"> <strong>' + e + '</strong></a></td></tr>');
}
}
// document.write('<tr><td colspan="3" cellspacing="0" cellpadding="1" align="center"><em>Back to top</em></td></tr>');
document.write('</tbody></table>');
}
}
Then it looks like they have cookies to determine if you've already agreed to the terms or not, so you can download all the file links.
It seems this is the download cookies code:
function writeSessionCookie (cookieName, cookieValue) {
if (testSessionCookie()) {
var d = new Date();
d.setTime(d.getTime() + (30*60*1000));
document.cookie = escape(cookieName) + "=" + escape(cookieValue) + "; domain=.oracle.com; path=/; expires=" + d.toGMTString();
return true;
}
else return false;
}
function getCookieValue (cookieName) {
var exp = new RegExp (escape(cookieName) + "=([^;]+)");
if (exp.test (document.cookie + ";")) {
exp.exec (document.cookie + ";");
return unescape(RegExp.$1);
}
else return false;
}
function testSessionCookie () {
document.cookie ="testSessionCookie=Enabled";
if (getCookieValue ("testSessionCookie")=="Enabled")
return true
else
return false;
}
function testPersistentCookie () {
writePersistentCookie ("testPersistentCookie", "Enabled", "minutes", 1);
if (getCookieValue ("testPersistentCookie")=="Enabled")
return true
else
return false;
}
function writePersistentCookie (CookieName, CookieValue, periodType, offset) {
var expireDate = new Date ();
offset = offset / 1;
var myPeriodType = periodType;
switch (myPeriodType.toLowerCase()) {
case "years":
expireDate.setYear(expireDate.getFullYear()+offset);
break;
case "months":
expireDate.setMonth(expireDate.getMonth()+offset);
break;
case "days":
expireDate.setDate(expireDate.getDate()+offset);
break;
case "hours":
expireDate.setHours(expireDate.getHours()+offset);
break;
case "minutes":
expireDate.setMinutes(expireDate.getMinutes()+offset);
break;
default:
alert ("Invalid periodType parameter for writePersistentCookie()");
break;
}
document.cookie = escape(CookieName ) + "=" + escape(CookieValue) + "; expires=" + expireDate.toGMTString() + "; path=/";
}
function deleteCookie (cookieName) {
if (getCookieValue (cookieName)) writePersistentCookie (cookieName,"Pending delete","years", -1);
return true;
}
I tried using the javascript code in a jsfiddle, but it didn't seem to work, so I assumed I was missing something.
Any thoughts on how to achieve this effect?
With jQuery Cookie plugin:
if($.cookie("accepted")){
$("#terms").hide();
}
$('#accept').click(function() {
$.cookie("accepted", true, { expires: 9999 });
$(this).parent().fadeOut();
});
JSFiddle: http://jsfiddle.net/Tymek/jv6L8ncL/ (see External Resources)
First step would be to learn JavaScript. It's unlikely that you'll be able to implement functionality by copying and pasting.
For this purpose you'll need to know how to check/set cookies and do some DOM manipulation.
Your logic would be
Check if the accept cookie is set.
If it is set, replace all the text with the actual links. Done.
If it is not set, display the text instead of the links.
When the accept is clicked, set the cookie and do Step 2. Done.
You should be working in a particular process
HTML
<input type="radio" id="radio" value=true>
<button id="startDownload">Download</button>
// Function to set and get Cookie
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = "expires="+d.toUTCString();
document.cookie = cname + "=" + cvalue + "; " + expires;
}
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1);
if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
}
return "";
}
// Function to execute when user clicks on download button
$("#startDownload").click(function(){
// Condition to check radio button and cookie parameter
if($("#radio").val() && getCookie("check_radio"))
{
// Proceed to download
}
else if($("#radio").val())
{
setCookie("check_radio", true, 365); // Set Cookie
// Proceed to download
}
else
{
alert("Please confirm terms and condition");
}
});
The idea here is to create a sort of "EULA wall". For some reason, whenever I test this code if I don't have the cookie, it works wonderfully, the EULA pops up, the accept button pops up and the accept button gives me the cookie. However, when I have the cookie, nothing in the actual if part happens. I currently have three "commands" one that would bring up the button again (a visual debug of sorts) one that would pass text to a <p></p>, and one that would call a function that runs the open.window(); function:
</script>
<p id = "loc"></p>
<script>
function printButton(){
var btn = document.createElement("BUTTON");
var text = document.createTextNode("accept");
btn.appendChild(text);
btn.setAttribute("onclick", "setCookie('accept','yes',365)")
document.getElementById("loc").appendChild(btn);
}
</script>
<script>
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
var expires = "expires=" + d.toUTCString();
document.cookie = cname + "=" + cvalue + "; " + expires;
location.reload();
}
</script>
<script>
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') c = c.substring(1);
if (c.indexOf(name) != -1) return c.substring(name.length, c.length);
}
return "";
}
</script>
<p id = "testingstuff"></p>
<script>
function checkCookie() {
var accept = getCookie("accept");
if (accept != "") {
newWindow();
printButton();
document.getElementById("testingstuff").innerHTML = "testing";
} else {
displayEULA();
printButton();
}
}
checkCookie();
</script>
<script>
function newWindow(){
window.open("http://google.com","_self");
}
</script>
</body>
</html>
Seems bizarre that this sort of thing would happen but maybe I'm just missing something obvious.
I am using this js code to validate a proper email is enetered into a form. If not, an warning box pops up.
function validateForm() {
var valid = true;
var errMsg = "";
var email = document.emailform.email.value;
//var fname = document.emailform.fname.value;
var filter = /^([a-zA-Z0-9_.-])+#(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
//if(fname.length <= 0) {
//valid = false;
//errMsg += "Please provide a valid first name.\n";
//}
if(email.length <= 0) {
valid = false;
errMsg += "Email address is required.\n";
} else {
if (!filter.test(email)) {
valid = false;
errMsg += "Please provide a valid email address.\n";
}
}
if (errMsg.length > 0) {
alert(errMsg);
return false;
}
}
Before my closing I have this...
<script>$('#awform').submit(validateForm);</script>
That works just fine.
I have another javascript code that I generally enter before the closing body tag, which works fine under normal circumstances.
<script language="JavaScript" type="text/javascript"> cbr202=Math.random()*10000000000000000;document.write('<scr'+'ipt language="JavaScript" src="http://mywebsite.com/track/static/landing.php?lpip=123&202cb='+cbr202+'" type="text/javascript"></scr' + 'ipt>'); </script>
This last piece is for a custom analytics program I use.
The problem I'm running into is that when I use the two scripts together, the validation no longer works.
So I do...
<script language="JavaScript" type="text/javascript"> cbr202=Math.random()*10000000000000000;document.write('<scr'+'ipt language="JavaScript" src="http://mywebsite.com/track/static/landing.php?lpip=123&202cb='+cbr202+'" type="text/javascript"></scr' + 'ipt>'); </script>
<script>$('#awform').submit(validateForm);</script>
</body>
As requested, here's what the tracking script looks like....
function t202Init(){
//this grabs the cid, but if they set a forced kw, this will be replaced
if (readCookie('t202forcedkw')) {
var cid = readCookie('t202forcedkw');
} else {
var cid = t202GetVar('cid');
}
var lpip = '<? echo htmlentities($_GET['lpip']); ?>';
var campid = t202GetVar('campid');
var OVRAW = t202GetVar('OVRAW');
var OVKEY = t202GetVar('OVKEY');
var OVMTC = t202GetVar('OVMTC');
var c1 = t202GetVar('c1');
var c2 = t202GetVar('c2');
var c3 = t202GetVar('c3');
var c4 = t202GetVar('c4');
var target_passthrough = t202GetVar('target_passthrough');
var keyword = t202GetVar('keyword');
var referer = document.referrer;
var resolution = screen.width+'x'+screen.height;
var language = navigator.appName=='Netscape'?navigator.language:navigator.browserLanguage;
language = language.substr(0,2);
document.write("<script src=\"<?php echo $strProtocol; ?>://<? echo $_SERVER['SERVER_NAME']; ?>/jump/static/record.php?lpip=" + t202Enc(lpip)
+ "&campid=" + t202Enc(campid)
+ "&cid=" + cid
+ "&OVRAW=" + t202Enc(OVRAW)
+ "&OVKEY=" + t202Enc(OVKEY)
+ "&OVMTC=" + t202Enc(OVMTC)
+ "&c1=" + t202Enc(c1)
+ "&c2=" + t202Enc(c2)
+ "&c3=" + t202Enc(c3)
+ "&c4=" + t202Enc(c4)
+ "&target_passthrough=" + t202Enc(target_passthrough)
+ "&keyword=" + t202Enc(keyword)
+ "&referer=" + t202Enc(referer)
+ "&resolution=" + t202Enc(resolution)
+ "&language=" + t202Enc(language)
+ "\" type=\"text/javascript\" ></script>"
);
}
function t202Enc(e){
return encodeURIComponent(e);
}
function t202GetVar(name){
get_string = document.location.search;
return_value = '';
do {
name_index = get_string.indexOf(name + '=');
if(name_index != -1) {
get_string = get_string.substr(name_index + name.length + 1, get_string.length - name_index);
end_of_value = get_string.indexOf('&');
if(end_of_value != -1) {
value = get_string.substr(0, end_of_value);
} else {
value = get_string;
}
if(return_value == '' || value == '') {
return_value += value;
} else {
return_value += ', ' + value;
}
}
}
while(name_index != -1)
//Restores all the blank spaces.
space = return_value.indexOf('+');
while(space != -1) {
return_value = return_value.substr(0, space) + ' ' +
return_value.substr(space + 1, return_value.length);
space = return_value.indexOf('+');
}
return(return_value);
}
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
function eraseCookie(name) {
createCookie(name,"",-1);
}
t202Init();
Try switching the order of the two scripts. It seems that the custom analytics program is running document.write which is a bit nasty and may cause the rest of the document to not be parsed correctly.
Why is document.write considered a "bad practice"?.
Your embedded Javascript tags can break your script. Whenever I try to do a script of the form:
<script>
// Some stuff here
<script>
// More stuff here
</script>
</script>
The script breaks. Try using a different method to load your analytics.
Why not using jQuery to add the script-tag. You obviously have jQuery included.
Try
<script type="text/javascript">
var cbr202 = Math.random() *10000000000000000;
$('head').append('<scr'+'ipt type="text/javascript" src="http://mywebsite.com/track/static/landing.php?lpip=123&202cb='+cbr202+'" />');
</script>