SoFunction
Updated on 2025-04-08

A route written in js (simple example)

A few days ago, I saw a big guy writing a route using JS online. There was a sentence that I didn’t know what was going on. Later, I wrote one myself. It was rough. I think the object-oriented idea was messed up, but the function was implemented.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>jsImplement routing</title>
</head>
<body>
  < a href=" " >white</ a>
  < a href="#/green" >green</ a>
  < a href="#/blue" >blue</ a>
  < a href="#/yellow" >yellow</ a>
</body>
</html>

<script>

  function Route(){
  }
  =function(route,callback){
    var arr={};
    arr[route]=callback;

    ('hashchange',function(){
      var temp=;
      for(var i in arr){
        if(i==(1,)){
          arr[i]();
        }
      }
    })
  }
  =new Route();


  function change(color){
    var body=('body')[0];
    =color;
    (color);
  }
  
  ('/',function(){
    change('');
  });
  ('/green',function(){
    change('green');
  });
  ('/blue',function(){
    change('blue');
  });
  ('/yellow',function(){
    change('yellow');
  });

</script>

The above is all the content of a route (simple example) written in js brought to you by the editor. I hope it will be helpful to everyone and support me more~