dougou8552 2013-04-01 18:58
浏览 37

如何在javascript中的查询字符串中创建重定向页面?

I have a website which changes the content of the page depending on what the user puts into the url.

There is only one page, but it seems like there are more because of the .htaccess I have used.

The website goes

www.site.com/1 or www.site.com/482

the content then changes depending on what the url is, for example /1 would load content assigned to ID 1 and /482, load data assigned to that of ID 482.

My problem is when the user just goes to www.site.com the page still loads but the youtube clip on the page displays a fuzzy screen with the message 'invalid parameters' which is clearly unprofessional. I want the user to be redirected to a random page within my website (anything from /1 to /999) when they go to www.site.com (when there is no forward slash and a number).

I have this code:
var randomnumber = Math.round((Math.random()) * 999 + 1);
var DATA = window.location.pathname.replace('/', '');
if (typeof DATA == "undefined"){
    DATA = randomnumber;
}

but it doesn't seem to change anything

  • 写回答

2条回答 默认 最新

  • duannaxin9975 2013-04-01 19:07
    关注

    If you want to redirect using javascript, you've got to set location property of window object like this

    var randomnumber = Math.round((Math.random()) * 999 + 1);
    var DATA = window.location.pathname.replace('/', '');
    if ( DATA == ""){
        window.location = '/' + randomnumber;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上