te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - IS it safe to use window.location to query the GET params of a page? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - IS it safe to use window.location to query the GET params of a page? - Stack Overflow

programmeradmin3浏览0评论

I'm doing a peer review and I've found people using window.location.search to check what paremetes have been sent to a given (search) page.

Is it safe to do so? I was thinking that we could probably print the parameters in the HTML output inside a script block and verify the printed variables instead of querying window.location.

I'm doing a peer review and I've found people using window.location.search to check what paremetes have been sent to a given (search) page.

Is it safe to do so? I was thinking that we could probably print the parameters in the HTML output inside a script block and verify the printed variables instead of querying window.location.

Share Improve this question asked Jan 20, 2009 at 16:41 Jj.Jj. 3,16026 silver badges31 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 13

If javascript is enabled, window.location.search is safe to use.

And just as some useless piece of further information: The property was as far as I know introduced in Netscape Navigator 2 / MS Internet Explorer 3, so I'd say it's pretty safe to use, even if it's not part of any standard (yet).

One thing to note about this approach. window.location is set statically on page load and will not detect changes that the user has made to the address bar after that time. This should not be a concern but it is important to know.

Save the following code as an html file and fire it up in a browser:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
    "http://www.w3/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <title>test</title>
    </head>
    <body>
        <a href="javascript:void(0);" 
                onclick="alert(window.location);">click me</a>
    </body>
</html>

The "click me" anchor will display the current window location onclick. However if you add anything to the address bar and click the link again it will report the same thing it did the first time.

Hopefully this is not a concern and I cannot imagine that it would affect you in any way but it is good to know.

Safe as in 'security' or 'will this work always' ?

Even though window.location is widely it is still not part of the W3C standard. However it was added to the working draft spec in 2006: basically means a browser may or may not support it. So from a 'will this work always' you will be taking a small chance I guess.

Is it safe to do so?

Yes. Example code to get parameters as a name->value map, assuming you don't need multiple values per parameter:

function getParameters() {
    var parameters= new Object();
    var parts= window.location.search.substring(1).split('\x26');
    for (var parti= parts.length; parti-->0;) {
        var subparts= parts[parti].split(';'); // support semicolon separators as well as ampersand (see HTML 4.01 section B.2.2)
        for (var subparti= subparts.length; subparti-->0;) {
            var parparts= subparts[subparti].split('=', 2);
            if (parparts.length==2)
                parameters[decodeURIComponent(parparts[0])]= decodeURIComponent(parparts[1]);
        }
   }
   return parameters;
}
发布评论

评论列表(0)

  1. 暂无评论