Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: if else function in internet explorer - not working.

  1. #1
    Join Date
    Apr 2007
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default if else function in internet explorer - not working.

    I have a problem that I can't seem to resolve by myself. Hopefully someone can help.

    I have a text field that when someone enters a keyword, the window goes to a different website depending on what is entered into the field.

    Here is my function called iframe. I call it like this: <body onLoad="iframe()">


    It works great in Firefox, but doesn't work in internet explorer.

    I am sure there is an easy solution, I just doesn't know what it is. Is there something about explorer that I don't know about preventing this code from working?

    Ive tried using window.location instead of document.location and that doesn't work either.

    Thanks to anyone that can help me out!



    function iframe() {
    if(document.forms["keyword"].txt1.value.toLowerCase() == ""){
    }

    else if(document.forms["keyword"].txt1.value.toLowerCase() == "yahoo"){
    document.location='http://www.yahoo.com';}

    else if(document.forms["keyword"].txt1.value.toLowerCase() == "google"){
    document.location = 'http://www.google.com';}

    else document.location='http://www.csun.edu';}

  2. #2
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default

    It's window.location.href
    Trinithis

  3. #3
    Join Date
    Apr 2007
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default it still...

    doesn't work

    However, it still works in Firefox.

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Just off the top of my head -

    Why in the world would you name a function after an element?
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Apr 2007
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    It didn't even cross my mind that it could be a problem.

    Do you think that could be causing the function not to work???

  6. #6
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    It is very possible. Your best bet would be to rename the function. As for the code, there shouldn't be anything wrong with it (just looking over it briefly).

    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  7. #7
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default

    This code works in both IE and FF
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html>
    <head>
    <title>Test</title>
    </head>
    
    <body>
    <div>
    
    <input type="text" id="txt1" />
    <input type="button" id="btn1" value="Go" />
    
    <script type="text/javascript">
    
    function changeLocation(e) {
    	var val = document.getElementById("txt1").value.toLowerCase().replace(/^\s+/, "").replace(/\s+$/, "");
    	if(val.indexOf("http://")==0) {
    		window.location.href = val;
    		return;
    		}
    	if(val.indexOf("www.")==0) {
    		window.location.href = "http://" + val;
    		return;
    		}
    	switch(val) {
    		case "":
    			return;
    		case "google":
    			window.location.href = "http://www.google.com";
    			return;
    		case "yahoo":
    			window.location.href = "http://www.yahoo.com";
    			return;
    		default:
    			window.location.href = "http://www.csun.edu";
    		}
    	}
    
    var btn = document.getElementById("btn1");
    
    btn.addEventListener? btn.addEventListener("click", changeLocation, false): btn.attachEvent("onclick", changeLocation);
    
    </script>
    </div>
    </body>
    </html>
    Trinithis

  8. #8
    Join Date
    Apr 2007
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I changed the function name, but it still doesn't work in explorer.

    I'm thinking it has something to do with the body onload event

    Here is my entire page, with the javascript function called textfield in the external js file (keywords.js).


    <html>
    <head>
    <title></title>
    <script type="text/javascript" src="keywords.js"></script>

    </head>


    <body onLoad="textfield();dropdown()">

    <form action="/frame.html" onClick="rememberinput('txt1')" method="get" name="keyword" id="keyword">
    <p align="center"><Br>
    <input name="txt1" id="txt1" type="text" size="30" class="memorize">
    <input type="button" value="Verify" onClick="textfield();">
    <input name="t2" type="" value="" size="1" style="visibility:hidden";>
    </p>
    </form>
    </p>
    <p></p>
    </body>
    </html>


    and here is the function in keywords.js

    function textfield() {
    if(document.forms["keyword"].txt1.value.toLowerCase() == ""){
    }

    else if(document.forms["keyword"].txt1.value.toLowerCase() == "yahoo"){
    window.location.href='http://www.yahoo.com';}

    else if(document.forms["keyword"].txt1.value.toLowerCase() == "google"){
    window.location.href = 'http://www.google.com';}

    else window.location.href='http://www.csun.edu';}
    Last edited by kimberlyhello; 09-23-2007 at 01:59 AM.

  9. #9
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    It looks as though Trinithis has taken the time to come up with tested code:

    Quote Originally Posted by Trinithis
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html>
    <head>
    <title>Test</title>
    </head>
    
    <body>
    <div>
    
    <input type="text" id="txt1" />
    <input type="button" id="btn1" value="Go" />
    
    <script type="text/javascript">
    
    function changeLocation(e) {
    	var val = document.getElementById("txt1").value.toLowerCase().replace(/^\s+/, "").replace(/\s+$/, "");
    	if(val.indexOf("http://")==0) {
    		window.location.href = val;
    		return;
    		}
    	if(val.indexOf("www.")==0) {
    		window.location.href = "http://" + val;
    		return;
    		}
    	switch(val) {
    		case "":
    			return;
    		case "google":
    			window.location.href = "http://www.google.com";
    			return;
    		case "yahoo":
    			window.location.href = "http://www.yahoo.com";
    			return;
    		default:
    			window.location.href = "http://www.csun.edu";
    		}
    	}
    
    var btn = document.getElementById("btn1");
    
    btn.addEventListener? btn.addEventListener("click", changeLocation, false): btn.attachEvent("onclick", changeLocation);
    
    </script>
    </div>
    </body>
    </html>
    to apply to this situation, did you try it?

    In any case, this looks really bad to me:

    Quote Originally Posted by kimberlyhello
    Code:
    if(document.forms["keyword"].txt1.value.toLowerCase() == ""){
    }
    Once again, off the top of my head, if you are constructing a test:

    Quote Originally Posted by kimberlyhello
    Code:
    if(document.forms["keyword"].txt1.value.toLowerCase() == ""
    you should do something as a result. If nothing else, return:

    Code:
    if(document.forms["keyword"].txt1.value.toLowerCase() == ""){
    return;
    }
    Still, I would recommend that you at least try the solution offered by Trinithis.


    Your onload event looks fine, provided that the function:

    Code:
    dropdown();
    exists and is error free.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  10. #10
    Join Date
    Apr 2007
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thank you so much to everyone that is trying to help me.

    I did try the code Trinithis provided, but it still doesn't work in Explorer. BTW, thank you Trinithis for taking your time to write that code for me. I was hoping it would work, but I'm still having the same problem.

    I think it would be easier to show you the website I am talking about.

    Go to http://www.googlegive.com

    Type into the 2nd textfield box, "autism speaks" - then click verify. Now refresh the browser and see what happens.

    If you are using firefox...a cookie is enable and the window is redirected to the appropriate place. It works perfectly in firefox.

    If you are using explorer...the page is not redirected. However, the cookie seems to work because the textfield will still contain what you typed in-"autism speaks"

    Please if you can, check it out in both browsers and you will see what I'm talking about.

    I just need to fix it in Explorer.

    I hope this explanation isn't too confusing. I've tried my best to fix it myself and have had no luck.

    Thanks again!

    Also, here is the javascript code contained in keywords.js




    function googlegive() {
    if(document.forms["keyword"].txt1.value.toLowerCase() == ""){
    return;}

    else if(document.forms["keyword"].txt1.value.toLowerCase() == "make a wish"){
    window.location.href='http://www.googlegive.com/charity/makeawish.html';}

    else if(document.forms["keyword"].txt1.value.toLowerCase() == "autism speaks"){
    window.location.href='http://www.googlegive.com/charity/autismspeaks.html';}

    else window.location.href='http://www.googlegive.com/charity/nocharity.html';}




    var remembervalues_days=60
    var dyncookiename="keyword"

    var recallinput=function(){
    var cookienamevalue=rememberinput.getCookie(dyncookiename).split("##")
    for (var i=0; i<cookienamevalue.length; i++){
    var cookiename=cookienamevalue[i].split("#")[0]
    var cookievalue=cookienamevalue[i].split("#")[1]
    if (document.getElementById(cookiename)!=null && document.getElementById(cookiename).type=="text")
    document.getElementById(cookiename).value=decodeURI(cookievalue)
    }
    }

    var rememberinput=function(){
    var rememberit=""
    for (var i=0; i<arguments.length; i++){
    if (document.getElementById(arguments[i]).type=="text" && document.getElementById(arguments[i]).value!="")
    rememberit+=arguments[i]+"#"+encodeURI(document.getElementById(arguments[i]).value)+"##"
    }
    rememberinput.setCookie(dyncookiename, rememberit, remembervalues_days)
    }

    rememberinput.getCookie=function(Name){
    var re=new RegExp(Name+"=[^;]+", "i");
    if (document.cookie.match(re))
    return document.cookie.match(re)[0].split("=")[1]
    return ""
    }

    rememberinput.setCookie=function(name, value, days){
    var expireDate = new Date()

    var expstring=expireDate.setDate(expireDate.getDate()+parseInt(days))
    document.cookie = name+"="+value+"; expires="+expireDate.toGMTString()+"; path=/;"
    }

    if (window.addEventListener)
    window.addEventListener("load", recallinput, false)
    else if (window.attachEvent)
    window.attachEvent("onload", recallinput)
    else if (document.getElementById)
    window.onload=recallinput





    function StateSuggestions() {
    this.states = [
    "a better chance", "accent On Success", "acres of hope", "aid for cancer research", "alameda creek alliance", "alaska assistance dogs","all charities", "autism speaks",
    "breast cancer", "burts charity",
    "cancer", "charity", "cure",
    "Delaware", "Florida", "Georgia", "Hawaii",
    "Idaho", "Illinois", "Indiana", "Iowa",
    "Kansas", "Kentucky", "Louisiana",
    "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota",
    "Mississippi", "Missouri", "Montana",
    "Nebraska", "Nevada", "New Hampshire", "New Mexico", "New York",
    "North Carolina", "North Dakota", "Ohio", "Oklahoma", "Oregon",
    "Pennsylvania", "Rhode Island",
    "South Carolina", "South Dakota",
    "Tennessee", "Texas", "Utah", "Vermont", "Virginia",
    "Washington", "West Virginia", "Wisconsin", "Wy"
    ];
    }


    AutoSuggestControl.prototype.handleKeyDown = function (oEvent /*:Event*/) {

    switch(oEvent.keyCode) {
    case 38: //up arrow
    this.previousSuggestion();
    break;
    case 40: //down arrow
    this.nextSuggestion();
    break;
    case 13: //enter
    this.hideSuggestions();
    break;
    }

    };


    AutoSuggestControl.prototype.handleKeyUp = function (oEvent /*:Event*/) {

    var iKeyCode = oEvent.keyCode;


    if (iKeyCode == 8 || iKeyCode == 46) {
    this.provider.requestSuggestions(this, false);


    } else if (iKeyCode < 32 || (iKeyCode >= 33 && iKeyCode < 46) || (iKeyCode >= 112 && iKeyCode <= 123)) {

    } else {

    this.provider.requestSuggestions(this, true);
    }
    };


    AutoSuggestControl.prototype.hideSuggestions = function () {
    this.layer.style.visibility = "hidden";
    };


    AutoSuggestControl.prototype.highlightSuggestion = function (oSuggestionNode) {

    for (var i=0; i < this.layer.childNodes.length; i++) {
    var oNode = this.layer.childNodes[i];
    if (oNode == oSuggestionNode) {
    oNode.className = "current"
    } else if (oNode.className == "current") {
    oNode.className = "";
    }
    }
    };


    AutoSuggestControl.prototype.init = function () {


    var oThis = this;


    this.textbox.onkeyup = function (oEvent) {


    if (!oEvent) {
    oEvent = window.event;
    }


    oThis.handleKeyUp(oEvent);
    };


    this.textbox.onkeydown = function (oEvent) {


    if (!oEvent) {
    oEvent = window.event;
    }


    oThis.handleKeyDown(oEvent);
    };


    this.textbox.onblur = function () {
    oThis.hideSuggestions();
    };


    this.createDropDown();
    };


    AutoSuggestControl.prototype.nextSuggestion = function () {
    var cSuggestionNodes = this.layer.childNodes;

    if (cSuggestionNodes.length > 0 && this.cur < cSuggestionNodes.length-1) {
    var oNode = cSuggestionNodes[++this.cur];
    this.highlightSuggestion(oNode);
    this.textbox.value = oNode.firstChild.nodeValue;
    }
    };


    AutoSuggestControl.prototype.previousSuggestion = function () {
    var cSuggestionNodes = this.layer.childNodes;

    if (cSuggestionNodes.length > 0 && this.cur > 0) {
    var oNode = cSuggestionNodes[--this.cur];
    this.highlightSuggestion(oNode);
    this.textbox.value = oNode.firstChild.nodeValue;
    }
    };


    AutoSuggestControl.prototype.selectRange = function (iStart /*:int*/, iLength /*:int*/) {


    if (this.textbox.createTextRange) {
    var oRange = this.textbox.createTextRange();
    oRange.moveStart("character", iStart);
    oRange.moveEnd("character", iLength - this.textbox.value.length);
    oRange.select();


    } else if (this.textbox.setSelectionRange) {
    this.textbox.setSelectionRange(iStart, iLength);
    }


    this.textbox.focus();
    };


    AutoSuggestControl.prototype.showSuggestions = function (aSuggestions /*:Array*/) {

    var oDiv = null;
    this.layer.innerHTML = "";

    for (var i=0; i < aSuggestions.length; i++) {
    oDiv = document.createElement("div");
    oDiv.appendChild(document.createTextNode(aSuggestions[i]));
    this.layer.appendChild(oDiv);
    }

    this.layer.style.left = this.getLeft() + "px";
    this.layer.style.top = (this.getTop()+this.textbox.offsetHeight) + "px";
    this.layer.style.visibility = "visible";

    };


    AutoSuggestControl.prototype.typeAhead = function (sSuggestion /*:String*/) {


    if (this.textbox.createTextRange || this.textbox.setSelectionRange){
    var iLen = this.textbox.value.length;
    this.textbox.value = sSuggestion;
    this.selectRange(iLen, sSuggestion.length);
    }
    };

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •