-1

I want to distinguish the values entered by the user.

Users use an input form to add values.

If '#' is the front of the value entered by the user. remove '#' and redirect to A

other than that redirect to B

I tried to distinguish this by using charAt() but do not work

also i don't know .replace() is working

That code only goes to the current URL + /?, whatever i type.

please help me

<form>
  <input id="pathID" placeholder="Search ..." +
         style="height:40px; line-height:40px; padding:0 15px;"> 
  <input onclick="return findSharp()" type="submit" 
         value="Search" style="background:#222;">
</form>
<script>
  function findSharp(){
    var stringName = document.getElementById("pathID").value;
    if(stringName.charAt(0) == '#'){
      findURLA()
    } else {
      findURLB()
    }
  }
                       
  function findURLA(){
    var hashId = document.getElementById("pathID").value;
    var hashIdReplace = hashId.replace('#','');
    window.location.href = 
      'http://localhost:8181/activity-2/' + '?' + 
      'hashtag/' + hashIdReplace;
      return false;
  }
                       
  function findeURLB(){
    window.location.href = 'http://localhost:8181/';
    document.write("I AM URL B");
    return false;
  }
</script>
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
Kimchiman
  • 19
  • 3
  • You are using the `click` event of a `submit` button and both potential return values are `false`, meaning that you always want to cancel the `click`. In this case, you really shouldn't be using a `form` and `submit` button in the first place. – Scott Marcus Sep 26 '19 at 05:18
  • You have a typo in code : `findURLB()` and function `findeURLB()` note the **e** that's the problem. – Divyesh patel Sep 26 '19 at 05:21
  • other wise code is works. – Divyesh patel Sep 26 '19 at 05:22

3 Answers3

0

Your code is working fine on me, also tested, the only thing is that it has an error:

function findeURLB(){

it should be:

function findURLB(){

UPDATE:

you might try this

Shiz
  • 695
  • 10
  • 27
  • did it work now? It works on me when I fixed the typo error – Shiz Sep 26 '19 at 05:45
  • i'm try to 'Rinkesh Golwala' 's answer and fixed the type error, findURLA is worked but findURLB is do not work... – Kimchiman Sep 26 '19 at 05:51
  • @Kimchiman I don't actually know what was wrong, but you can try the update that I made. It shows A and B are working. – Shiz Sep 26 '19 at 06:04
  • your code is working on that site, but B do not working on my system...T.T I don't know why don't worked B in my system.../ i type "#test'. A is good, i type 'test' and click seach button but don't act search button... – Kimchiman Sep 26 '19 at 06:19
-1

You can try this.

          function findSharp(){
                var stringName = 
                document.getElementById("pathID").value;
                if(stringName[0] == '#'){
                    findURLA(stringName.slice(1,))
                }
                else{
                    findURLB(stringName)
                }
            }

            function findURLA(hashId){
                window.location.href = 'http://localhost:8181/activity-2/' + '?' + 'hashtag/' + hashId;
            }

            function findURLB(hashId){
                window.location.href = 'http://localhost:8181/';
                document.write("I AM URL B");
            }
Rinkesh Golwala
  • 979
  • 1
  • 7
  • 17
-1

I change a little bit your code in few places

  • give event and event.preventDefault() to findSharp function for remove return statement from html (more here)
  • add parameters to findURLAA and findURLB (and fix typeo error in that second function name) to omit repeated reading and remove coupling thath functions with global/html variables
  • temporary comment findURL-A/B content - you can uncomment it

function findSharp(event) {
  var path = pathID.value;

  if (path[0] == '#') {
    findURLA(path)
  } else {
    findURLB(path)
  }
  event.preventDefault(); // not change location by form
}

function findURLA(path) {
  var hashIdReplace = path.replace('#', '');
  console.log('A:', hashIdReplace)
  // window.location.href = 'http://localhost:8181/activity-2/' + '?' + 'hashtag/' + hashIdReplace;
}

function findURLB(path) {
  console.log('B:', path)
  //window.location.href = 'http://localhost:8181/';
  //document.write("I AM URL B");

}
<form>
  <input id="pathID" placeholder="Search ..." style="height:40px; line-height:40px; padding:0 15px;">
  <input onclick="findSharp(event)" type="submit" value="Search">
</form>
Kamil Kiełczewski
  • 85,173
  • 29
  • 368
  • 345