I have (what to me anyway is) a tricky javascript if/else statement I need to include in the checkout page of an ecommerce site. I have the basics but I guess I'm missing something because it's not working. The code just shows up in the page source of the browser in all its glory (showing BOTH statements instead of just whichever one should show up). The basics are as follows: <script type="text/javascript"> if (document.cookie.indexOf('SOURCE') != -1) { document.write('html code for this condition goes here'); } else { document.write('html code for this condition goes here'); } </script> Code (markup): Is this correct or am I missing something? I'm checking for the existence of a value on the browser cookie SOURCE and if that exists then document.write() the first set of code, if it does not exist then show the second set of code. Thanks in advance.
Since the text being written is the same in either case, there's no way to tell which condition fired. Try <script type="text/javascript"> if (document.cookie.indexOf('SOURCE') != -1) { document.write('html code for "found" goes here'); } else { document.write('html code for "not found" goes here'); } </script> Code (markup): (It's a common mistake - you concentrated on the code so much that you missed the simple problem.)
Thanks for the reply! I actually have code inside of there but I did not want to post it here. I tried a simple example taking other scripts out of the conditional areas but still the code is all just displayed in the page source in the browser. With javascript if/else statements, would it not just display the correct document.write() value and not both of them? I'm fairly confused.
FYI i'm happy to pay someone that can get this working correctly for me. Please PM me for specifics and I will show you exactly what code each condition needs to execute.
So evidently you wrote representative code here, rather than copying and pasting the code that's not working. No one can help you when you ask for help to fix code that's not actually broken. Copy and paste the original code from the website and, if you don't want us to see that code, just change that text (don't change braces or any of the actual code itself). Changing just one punctuation mark in the code you show us could completely eliminate any possibility of our finding the error.
the use of indexOf could be returning false positives depending on what cookies are actually set. You'd be better off parsing out the cookies and checking against it. something like: function getCookie(name) { var prefix=name+"="; var begin=document.cookie.indexOf("; "+prefix); if (begin==-1) { begin=document.cookie.indexOf(prefix); if (begin!=0) return null; } else begin+=2; var end=document.cookie.indexOf(";",begin); if (end==-1) end=document.cookie.length; return unescape(document.cookie.substring(begin+prefix.length,end)); } Code (markup): Does all the checks to make sure you're getting the cookie and it's value -- returns NULL if not found.
Ok, quick update here and thanks everyone for their input. I'll post the actual code here with URL's xxxxx'd out for client privacy. What this needs to do is check for the existence of a SOURCE cookie and if it exists execute the first set of code and if it doesnt exist execute the second set of code: <script type="text/javascript"> if (document.cookie.indexOf('SOURCE') != -1) { document.write('<!-- BEGIN COMMISSION JUNCTION TRACKING CODE - DO NOT MODIFY --> <iframe height="1" width="1" frameborder="0" scrolling="no" src="https://www.emjcd.com/tags/c?containerTagId=1&CID=11263432&OID=%%ORDER_ID%%&AMT1=%%ORDER_SUBTOTAL_DISCOUNTED%%&CURRENCY=USD" name="Conversion tag"></iframe><!-- END COMMISSION JUNCTION TRACKING CODE.-->'); } else { document.write('<scr'+'ipt type="text/javascript">document.write(unescape("%3Cscript id=%27pap_x2s6df8d%27 src=%27" + (("https:" == document.location.protocol) ? "https://" : "http://") + "www.xxxxxxxxxxx.com/v4/scripts/trackjs.js%27 type=%27text/javascript%27%3E%3C/script%3E")); </scr'+'ipt> <scr'+'ipt type="text/javascript"> PostAffTracker.setAccountId('default1'); var sale = PostAffTracker.createSale(); sale.setTotalCost('%%ORDER_SUBTOTAL_DISCOUNTED%%'); sale.setOrderID('%%ORDER_ID%%'); PostAffTracker.register(); </scr'+'ipt>'); } </script> Code (markup): Again, I'm happy to hire someone to fix this and make it work if anyone can figure it out. Thanks all!
Hi embarq have you managed to find someone to correct your errors in JS. I am having problem with my JS script and Need some to correct it for me I am willing to pay for it.
I did, but not here. I posted an ad over at freelancer.com and got multiple responses and had the problem resolved within 2-3 hours very inexpensively.