Home > Net >  How to revert to windows if JS value is null
How to revert to windows if JS value is null

Time:12-11

I have like no JS experience, but I need help.

So I have a code that allows a user to manually enter a URL.

Well, If they leave the value blank and click out it "NULLS". I want a way to detect that null and revert back to "/" or the same page instead of /null

function url(){
    swal("Enter a URL:", {
        content: "input",
      })
      .then((value) => {
        location.replace(value);
        
      });
}
// PROD Fix, needs a zero-null failure-fix. Find a way to prevent zero-data/null.

CodePudding user response:

You can add a test for the null value before the location.replace() and change the value to the url you want as default.

function url(){
    swal("Enter a URL:", {
        content: "input",
      })
      .then((value) => {
        if (value === null) value = "your replacement url";
        location.replace(value);
        
      });
}

CodePudding user response:

Careful with the check if (value === null) if you can receive either null or an empty string. The condition will fail, if you receive an empty string.

I would use if (!value) instead to cover both null && "".

  • Related