weixin_33691700 2018-05-08 09:05 采纳率: 0%
浏览 96

如何重定向干净的URL

I am trying to redirect from javascript autocomplete search bar element.

Search field works perfectly except one thing. if i was in www.example.com/foo then click a data from search bar. Its redirects me to kinda this pages: the www.example.com/news/1

When i try searchbar again. And click products data from search bar. Its redirect me to the www.example.com/news/products/1

But i want to www.example.com/products/1

"/news" must go away.

if(window.location.pathname.split("/")[1] === "news"){

window.location.hostname.href ="?tab=" +ui.item.category_id+"&id="+ui.item.id;

}
else
window.location.href ="products?tab=" +ui.item.category_id+"&id="+ui.item.id;

window.location.hostname.href

didn't worked for me? How can i use clean link then add like "/products/ui.item.id"

my brief is i can't redirect (href) clean route.

  • 写回答

2条回答 默认 最新

  • weixin_33739541 2018-05-08 09:09
    关注

    You can use document.location.origin for this It will give you base url of page.

      window.location.href = window.document.location.origin + "products?tab=" + ui.item.category_id + "&id=" + ui.item.id;
    
    评论

报告相同问题?

悬赏问题

  • ¥15 java代码写在记事本上后在cmd上运行时无报错但又没生成文件
  • ¥15 关于#python#的问题:在跑ldsc数据整理的时候一直抱这种错误,要么--out识别不了参数,要么--merge-alleles识别不了参数(操作系统-linux)
  • ¥15 PPOCRLabel
  • ¥15 混合键合键合机对准标识
  • ¥100 现在不懂的是如何将当前的相机中的照片,作为纹理贴图,映射到扫描出的模型上
  • ¥15 魔霸ROG7 pro,win11.息屏后会显示黑屏,如图,如何解决?(关键词-重新启动)
  • ¥15 有没有人知道这是哪里出了问题啊?要怎么改呀?
  • ¥200 C++表格文件处理-悬赏
  • ¥15 Windows Server2016本地登录失败
  • ¥15 复合卡卡号轨道写入芯片卡