加载带有 url 哈希值的网站 [英] load website with hashs in the url
问题描述
我有一个网页视图,我可以在应用程序进入后台/退出时保存 URL,以便我可以保留用户在我们网站上的位置.
I've got a webview that I save the url from anytime the application goes to the background/exits so I can preserve our users location on our website.
我将用户输入的数据存储在 url 的主题标签中:
I store user-entered data in hashtags in the url:
https://test.org/?page=test#user_dat1=test#user_dat2=test
user_dat1 和 user_dat2 在哪里跟踪用户在哪里以及他们在页面上输入了什么.
Where user_dat1 and user_dat2 is keeping track of where the user is and what they have entered on a page.
当我使用以下内容时:
NSURL *url = [NSURL URLWithString:@"https://test.org/?page=test#user_dat1=test#user_dat2=test"];
[self.mywebview loadRequest:[NSURLRequest requestWithURL:url]];
我明白了:
https://test.org/?page=test%23user_dat1=test%23user_dat2=test
我的 php 和 js 都崩溃了.
Which crashes both my php and js.
如何强制不编码主题标签?
How can I force hashtags to not be encoded?
我在标记解决方案的指导下制作的代码解决方案
function read_hashes(){
try { // I know I shouldn't use try... but this is a easy way to reject invalid hashes
var hashes = window.location.hash;
hashes = hashes.substring(1);
hashes = JSON.parse(decodeURIComponent(hashes));
if (hashes["user_dat1"] != null){//check if var is in json hash
//do what you need to do with this var.
}
...
} catch(err){}
}
function put_hashes(){
var hashes = {};
hashes["user_dat1"] = "test data";
hashes = encodeURIComponent(JSON.stringify(hashes));
window.location.hash ="#" + hashes;
}
这允许我将变量存储在 js 哈希中,以将用户数据保存在表单中.请注意,我知道这是一个安全问题,我不会在任何会导致安全问题的页面上使用它.
This allows me to store variables in a js hash to keep user data in a form. And just a note, I know this is a security issue, I don't use this on any page that this would result in a security issue.
推荐答案
not with NSURL -- 如果未编码,则这是无效的 URL.
not with NSURL -- this is an invalid url if it wasn't encoded.
NSURL *url = [NSURL URLWithString:@"https://test.org/?page=test#user_dat1=test#user_dat2=test"];
返回 nil ==> 向您展示该字符串不是一个有效的网址
NSURL *url = [NSURL URLWithString:@"https://test.org/?page=test#user_dat1=test#user_dat2=test"];
returns nil ==> showing you the string is not a valid url
再次重申:NSURL 加载系统无法使用无效的 url.
to re-iterate: invalid urls are just not possible with NSURL Loading System.
使它无效的是两个散列.编码 1 然后修复您的解码并发送 test.org/?page=test#user_dat1=test%23user_dat2=test
what makes it invalid are the two hashes. Encode 1 then fix your decoding and send test.org/?page=test#user_dat1=test%23user_dat2=test
op 只想要他的本地 JS 中的片段,结果是.所以我建议在本地修复编码
the op only wants the fragment inside his local JS it turned out. So Id suggest fixing the encoding locally
这篇关于加载带有 url 哈希值的网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!