Alert Box Variables solution?

Discussion in 'JavaScript' started by enviomedia, Feb 2, 2011.

  1. #1
    Hello i hope i am in the right forum i have a ? i know how to make the alert box and have content show up in there but i am wondering how to lets say

    you click the link

    example http://www.somthing.com/index.html?kw=facebook

    and when the page loads the variable would show up in the alert box

    Congratulations Facebook Visitor ..... in the alert box...

    this is what i thought it would be but no luck..

    <script language="JavaScript">
    alert("Congratulations "+kw+"Visitor!\n\n You are the winner for "+this_date_string+" \n\n Please select a prize and enter your email on the next page to claim.");
    </script>

    Can this be done?

    Thanks In advance.
     
    enviomedia, Feb 2, 2011 IP
  2. Cash Nebula

    Cash Nebula Peon

    Messages:
    1,197
    Likes Received:
    67
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try this out :)

    example.html
    
    <html>
    <head>
    <script type="text/javascript">
    	function showPromo() {
    		var name = document.location.search;
    		if (name == "") return false;
    		var len = name.indexOf("=") - 1;
    		name = name.substr(1,len);
    		var date = new Date();
    		var weekDays = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
    		var day = weekDays[date.getDate()];
    		alert("Congratulations "+ name +" Visitor!\n\nYou are the winner for "+ day +". \n\nPlease select a prize and enter your email on the next page to claim.");
    	}
    </script>
    </head>
    <body onload="showPromo();">
    <a href="example.html?kw=facebook">Click Here</a>
    </body>
    </html>
    
    Code (markup):
     
    Cash Nebula, Feb 2, 2011 IP
  3. enviomedia

    enviomedia Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    the code it good but i need the alert box to open when the browser opens and win i click the link it says kw not facebook. >?
     
    enviomedia, Feb 2, 2011 IP
  4. Cash Nebula

    Cash Nebula Peon

    Messages:
    1,197
    Likes Received:
    67
    Best Answers:
    0
    Trophy Points:
    0
    #4
    No worries :) It also works when the page loads, I just added the link to make the demo work.
    
    <html>
    <head>
    <script type="text/javascript">
    function showPromoAlert() {
    	var site, idx, date, day;
    	site = document.location.search.toLowerCase();
    	if (site == "") return false;
    	idx = site.indexOf("kw=");
    	if (idx == -1) return false;
    	site = site.slice(idx + 3);
    	idx = site.indexOf("&");
    	if (idx > -1) site = site.substr(0, idx);
    	site = site.charAt(0).toUpperCase() + site.slice(1);
    	date = new Date();
    	weekDays = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
    	day = weekDays[date.getDate()];
    	alert("Congratulations "+ site +" Visitor!\n\nYou are the winner for "+ day +". \n\nPlease select a prize and enter your email on the next page to claim.");
    }
    </script>
    </head>
    <body onload="showPromoAlert();">
    <a href="example.html?kw=facebook&x=foo&y=bar">Click Here</a>
    </body>
    </html>
    
    Code (markup):
     
    Last edited: Feb 2, 2011
    Cash Nebula, Feb 2, 2011 IP
  5. GreenWithEnvy

    GreenWithEnvy Active Member

    Messages:
    125
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    53
    #5
    If your server is capable of PHP you could process it fairly simple from there too.

    You may want to think about putting a switch or if statement in to process the kw value, otherwise people could send malicious links that could end up with your site saying "Congratulations @#)$(#@$ Visitor!" (expletives)

    Not a serious loophole, but it's one worth considering.
     
    GreenWithEnvy, Feb 2, 2011 IP