Jump to content

Bare Bones JavaScript conditional text


RoedyGreen

Recommended Posts

I skimmed the JavaScript tutorial, but it did not teach me enough to solve what I think should be a very simple problem. I love learning by examples. They are so clear compared with exposition.

Here is what I need: There are thousands of people who do not know JavaScript, but are quite conversant in HTML and CSS . Everyone, who writes browser-detect or capabilities-detect software presumes their clients already know JavaScript. What we boobs need are a few examples of how to insert conditionality into HTML that does not require any server side code, just perhaps loading a JavaScript library into the client browser. Ideally the conditional blocks could be large, and would not need any special escaping.

E.g. for IE users, I want to display:

<!-- example wording for IE users -->You must <span class="click">click allow blocked content permission</span> toallow Active X to run. This also gives permission to Java to run.

For non-IE users I want to display:

<!-- example wording for non IE users -->You must <span class="click">click grant/accept</span> to allow Java to run.

Please show us everything we must embed in our HTML to make this work, including markup in the header and tail end to load JavaScript code.

I found this piece of code which looks like 90% of the solution:

// find out the version number of IE, or 0 if some other browser// minor variant of code at http://stackoverflow.com/questions/19999388/jquery-check-if-user-is-using-iefunction detectIE(){var ua = window.navigator.userAgent;var msie = ua.indexOf('MSIE ');var trident = ua.indexOf('Trident/');if (msie > 0){// IE 10 or older => return version numberreturn parseInt(ua.substring(msie + 5, ua.indexOf('.', msie)), 10);}if (trident > 0){// IE 11 (or newer) => return version numbervar rv = ua.indexOf('rv:');return parseInt(ua.substring(rv + 3, ua.indexOf('.', rv)), 10);}// other browserreturn 0;}

Unfortunately, JavaScript programmers consider it too obvious to ever show you what you do with this JavaScript to conditionally show two variants of the text. I get the impression you are supposed to embed the two alternate texts as String literals embedded in a chunk of JavaScript. Ouch!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...