views:

72

answers:

2

I've been trying to find a way to do this but, have not found many resources that specifically relate to this situation.

I need a javascript (can make use of Jquery if need be) to swap phone numbers on a site.

  • 2 numbers would be specified. One would show on the site by default.

  • The script needs to be able to create a cookie and do this by recognizing a value in the URL string.


For Example: http://www.site.com/index.html?value=

'?value=' would be the value.


This URL (with the additional variable) would bring the user to the same site but, the variable in the string would set the cookie and display a different number. The default number would not be displayed again until the cookies were removed.

Due to the situation with my server, I cannot make use of PHP ( I know, that would make things alot easier :P )

Any help would be greatly appreciated.

+1  A: 
Chad_K
@Chad_KThanks for your response. Can you fill in 2 fake phone numbers in the above example? I'm not 100% sure, where they are supposed to go.
Batfan
+1  A: 

UPDATED

DEMO:

GO here: http://jsbin.com/imawo4/85

THEN here: http://jsbin.com/imawo4/85/?value=john

include the jQuery Cookie Plugin

then use this:

HANDLE URL PARAMETER

  $.extend({
        getUrlVars: function(){
        var vars = [], hash;
        var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
         for(var i = 0; i < hashes.length; i++)
          {
        hash = hashes[i].split('=');
         vars.push(hash[0]);
        vars[hash[0]] = hash[1];
       }
      return vars;
 },
     getUrlVar: function(name){
     return $.getUrlVars()[name];
     }
  });

MAIN CODE

$(function() {
// SETTINGS
var identity = 'john';
var phone_number_1 = '555-555-555';
var phone_number_2 = '666-666-666';
var redirect = 'http://jsbin.com/imawo4/85/';

//MAIN SCRIPT
var url_query = $.getUrlVar('value');   
var coockie = $.cookie('phone_number');  
var swapped_phone_number = ( coockie == 'VALID' ) ? phone_number_1 : phone_number_2;
if ( url_query == identity && coockie != 'VALID' && url_query != undefined ) {
$.cookie( 'phone_number' , 'VALID' , { expires: 10 } );
window.location.href = redirect;}
if ( url_query != identity && coockie == null && url_query != undefined ) {
$.cookie( 'phone_number' , null );  
alert('INVALID');}
$('#the_one_already_here').text(swapped_phone_number);
});

HTML PART

<body>
<!--/ output numbers /-->
<div id="the_one_already_here"></div>
</body>

This should work as expected! ;-)

aSeptik
@aSeptikThanks for your post. How would I get this to work without the prompt? Like just have a number, add a variable to the URL, then have a different number?
Batfan
of course, the alert was just for Advice you of the change! but you can easily... se the update! ;-)
aSeptik
@aSeptikLooks awesome. The only thing I'm a little confused on is where to define the phone numbers?
Batfan
@aSeptik, also where do I enter the variable, for the URL?
Batfan
We have defined the COOKIE_NAME for easy access, then regarding the URL variable, this depend on what you are trying to do! from where this swap number come from!?
aSeptik
@aSeptikIt doesn't really matter, as long as I can specify 2 separate phone numbers in the javascript.I don't even want the 'swap my number link'. For example when I go to test.com/testpage.html I want 555-555-5555 displayed. When I go to test.com/testpage.html?paid= I want 555-666-6666 to be displayed. I would want ANY instance of the phone number on the page replaced.The cookie is to ensure that the number stays the same. Until the cookies are cleared, of course :)
Batfan
Ok, Ok! no problem ;-) you don't really need to use a link! for swap! you just need to point your browser to es.: http://mysite.com/?value=myclientname ... see the update!
aSeptik
@aSeptik , we're getting closer but, I cant test the new code because the page keeps refreshing over and over.
Batfan
try now! your url must be http://yoursite.com/?value=john
aSeptik
@aSeptik That works almost perfectly! :) The only thing is, the cookie doesnt seem to be working. After I have visited the page (with the default number) and then entered the value on the new page (with the new number), if I go back to the main page (no value) the number switches back. It should stay until I clear my cookies.
Batfan
Sorry for the delay, updated once again, try it now! ;-)
aSeptik