0

I have a problem with my current web application. Indeed, when I put the current link : https://localhost:44311/#shop, my page displayed perfectly. Here a picture : enter image description here

But then when I try to change my URL to : https://localhost:44311/#/ , to verify the control of the redirection, the content displayed into the same content, here a picture of what happened : enter image description here

Currently, all my ajax calls are called again and again and again (an infinite loop).

I'm trying to add control regex for #/, and try to respect #/xxxx/xxxxx. But without success. If you can help me, it could be very nice ! Here my JS where I manage the loadpages :

var ui = {
   controller: {
       page: {}
   },
   component: {
       category: null
   }
};
ui.controller.pageLoad = function (hash = null) {
   if (hash === null) {
       hash = window.location.hash;
       if (hash.length < 2 || hash[0] !== "#") {
           hash = "/shop";
       }
   } else if (hash.length < 2 || hash[0] === "#" && hash[1] === '/') { // add control regex for #/, must respect #/xxxx/xxxxx
       hash = "/shop";
   }

   $.get(hash.substring(1), function (data) {
       ui.controller.page = null;
       $("#content").html(data);
   }).fail(function (error) {
       $.get("/Home/Error?status=" + error.status, function (data) {
           ui.controller.page = null;
           $("#content").html(data);
       });
   });
};
0

1 Answer 1

1

You could create a isValidHash function, and use it in your if statement:

function isValidHash(hash) {
  return typeof hash === "string" && /^#(\/[a-z0-9-]+)+$/.test(hash);
}

function test(v) {
  console.log(`'${v}' is ${isValidHash(v) ? '' : 'NOT '}valid`);
}

test(null);            // NO - not a string
test('#');             // NO - too short
test('#shop');         // NO - no slash
test('#/shop');        // YES
test('#/shop/item-9'); // YES

Sign up to request clarification or add additional context in comments.

2 Comments

Oh yes thank you it works. Have you some link to share with me Sir about the regEx ? I'm still a beginner ahah. Thanks again!
Sure, you can go here, see that regex in action, and on the right you'll find the explanation of everything in this regex

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.