Okay, so I've created a JavaScript form which allows the user to enter their name into the form and display it within the sentence, "Welcome to the site ______". It works on one page, but, problem is, I'd like it to be displayed on every page. I'm aware you cannot do this with getElementbyId (can only be used once), which is what I've used. I've tried using getElementsbyClass, but from my understanding, it's not as simple as that.
Here's my code so far anyway:
Code:
<script type="text/javascript">
function inputname(){
var userInput = document.getElementById('userInput').value;
document.getElementById('welcome').innerHTML = userInput;
}
</script>
Code:
<p>Welcome to the site <b id='welcome'></b> </p>
<input type='text' id='userInput' />
<input type='button' onclick='inputname()' value='Enter Name'/>
To pass information of this kind from one page to another you should use a cookie. This has been covered many times in the forum.
Code:
<script type="text/javascript">
function changeText2(){
var userInput = document.getElementById('uInput').value;
userInput = userInput.replace(/[^a-z\s\-\']/gi,""); // letters space hyphen apostrophe only
userInput = userInput.toLowerCase().replace(/\b[a-z]/g,function(w){return w.toUpperCase()});
userInput = userInput + "!";
document.getElementById('boldStuff2').innerHTML = userInput;
document.getElementById("stuff").style.display="none";
createCookie ("UserName",userInput,365); // 365 days persistence
}
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=/";
}
// eraseCookie("UserName"); // use for testing - uncomment to erase the cookie
function eraseCookie(name) {
createCookie(name,"",-1);
}
// Everything below here excpt the lines in blue goes in your other pages as well as the home page.
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;
}
</script>
</head>
<body>
<p>Welcome to the site <b id='boldStuff2'></b> </p>
<div id = "stuff" style="display:block">
Please enter your name here <input type='text' id='uInput' onchange = "changeText2()">
</div>
<script type="text/javascript">
// this script must come after the divs- best to place it immediately in front of the </body> tag
if(readCookie('UserName')) {
var n = readCookie('UserName');
document.getElementById("boldStuff2").innerHTML = n;
document.getElementById("stuff").style.display="none";}
</script>
All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
__________________
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Okay, so I've created a JavaScript form which allows the user to enter their name into the form and display it within the sentence, "Welcome to the site ______".
A cookie is the last way I would use to transfer the person's name to another page unless you are restricted to doing this client-side only. The disadvantage of using cookies is that if the user has cookies disabled or clears the browser cache for some reason while on your website, the person's name will not appear on the page.
If you want to make sure the person's name appears every time then you need to store the person's name in a server side session variable and then use it to display the name on every page as required. This will work every time whether cookies are enabled or not.
If you want to make sure the person's name appears every time then you need to store the person's name in a server side session variable and then use it to display the name on every page as required. This will work every time whether cookies are enabled or not.
??? REALLY? PHP supports cookie-less sessions??
Hmmm...yes, reading the docs, I see that it does! How does it do that? Pass the session id along as a query string when cookies are disabled?
I knew ASP.NET allowed this. It can pass the session id info 3 ways: In cookie, in querystring, or in a hidden <form. field. Since all normal pages in ASP.NET utilize a <form>, this works.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Last edited by Old Pedant; 03-21-2012 at 03:59 AM..
Ah, but ASP only supports sessions via cookies. And JSP requires that you write a plugin to use anything but cookies. So your statement was really only true for ASP.NET and PHP (or JSP, *if* you are competent enough in Java).
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Ah, but ASP only supports sessions via cookies. And JSP requires that you write a plugin to use anything but cookies. So your statement was really only true for ASP.NET and PHP (or JSP, *if* you are competent enough in Java).
I don't see the point you are making, so all I can do is repeat my reply:
Quote:
I didn't even mention php
I was making a general suggestion to make sure the op is at least aware there is a better server side solution and so my work in this thread is done
A cookie is the last way I would use to transfer the person's name to another page unless you are restricted to doing this client-side only. The disadvantage of using cookies is that if the user has cookies disabled or clears the browser cache for some reason while on your website, the person's name will not appear on the page..
Well, in that case cookies are useless for any purpose as of course the user can always disable or erase them, or may have disabled Javascript. Many sites such as onine banking require that cookies are enabled. Here the worst that that can happen is that the user's name is not shown.
I would advise ACM to ignore this silly objection. "He only does it to annoy because he knows it teases" - Lewis Carroll
__________________
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Well, in that case cookies are useless for any purpose....
Not necessarily. I certainly wouldn't use cookies for critical features, but for non critical features I might use them.
If my name didn't appear when I logged into my bank account I would wonder if somehow the account has been compromised or whether security has been breached somehow. My online bank doesn't use cookies to display my name when I am logged in.
But like I said, I just want to make sure the op is aware there is a better server side solution but they can choose however they like to display the user's name
There are two methods to propagate a session id:
■ Cookies
■ URL parameter
The session module supports both methods. Cookies are optimal, but because they are not always available, we also provide an alternative way. The second method embeds the session id directly into URLs.
__________________
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
The session module supports both methods. Cookies are optimal, but because they are not always available, we also provide an alternative way. The second method embeds the session id directly into URLs.
That red bit is what I am suggesting, especially for critical features.
Last edited by webdev1958; 03-21-2012 at 09:41 AM..
That red bit is what I am suggesting, especially for critical features.
Displaying the visitor's name on ACM's website is hardly critical. And I have the idea that you might as well have made your suggestion in Klingon as far as ACM (1 post here) is concerned. I know you want to show everyone how amazingly clever you are, but you ought to take account of the OP's knowledge and ability range. You know perfectly well that many ISPs offering basic website hosting packages do not include a cgi-bin or access to server-side languages. So ACM is almost certianly restricted to doing this client-side only. Presumably he can work out whose suggestion is the most helpful to him.
__________________
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Displaying the visitor's name on ACM's website is hardly critical.
To you it obviously doesn't appear to be, but I doubt the op needs you to decide for him/her . I'm sure they can decide for themselves.
Like I said, I just want to make sure the op is aware there is a better server side solution but they can choose however they like to display the user's name
Quote:
Originally Posted by Philip M
You know perfectly well that many ISPs offering basic website hosting packages do not include a cgi-bin or access to server-side languages.
No, I'm not sure that is correct at all especially after you claimed recently that IE9 does not support document.getElementsByClassName() when it clearly does.
btw - Awardspace, for one, provides very good free web hosting
Last edited by webdev1958; 03-21-2012 at 09:59 AM..
Like I said, I just want to make sure the op is aware there is a better server side solution but they can choose however they like to display the user's name
Yes, sure, they can display it in Arabic or Chinese script if they wish. They do not need your graciously granted permission to choose to do what they want!
__________________
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.