javascript

url的三个js编码函数escape(),encodeURI(),encodeURIComponent()简介【转】

本小妞迷上赌 提交于 2021-02-20 19:49:30
为啥会有浏览器编码这一说法 一般来说,URL只能使用英文字母、阿拉伯数字和某些标点符号,不能使用其他文字和符号。比如,世界上有英文字母的网址 “h ttp://www.haorooms.com”, 但是没有希腊字母的网址“http://www.aβγ.com” (读作阿尔法-贝塔-伽玛.com)。这是因为网络标准RFC 1738做了硬性规定: 原文: "...Only alphanumerics [0-9a-zA-Z], the special characters " $ - _ .+!* '()," [not including the quotes - ed], and reserved characters used for their reserved purposes may be used unencoded within a URL." 翻译:“只有字母和数字[0-9a-zA-Z]、一些特殊符号“$-_.+!*'(),”[不包括双引号]、以及某些保留字,才可以不经过编码直接用于URL。” 这意味着,如果URL中有汉字,就必须编码后使用。但是麻烦的是,RFC 1738没有规定具体的编码方法,而是交给应用程序(浏览器)自己决定。这导致“URL编码”成为了一个混乱的领域。 出现浏览器编码的几种情况 1、网址路径中包含汉字 如下图: h ttp://www.haorooms

XHR 对象实例所有的配置、属性、方法、回调和不可变值

雨燕双飞 提交于 2021-02-20 19:46:25
当我们声明了一个XMLHttpRequest对象的实例的时候,使用for-in来循环遍历一下这个实例(本文使用的是chrome45版本浏览器),我们会发现在这个实例上绑定了一些内容,我把这些内容进行了一下分类: 一、配置项 也就是对xhr对象进行配置。 ① timeout : 配置请求超时时间。 ② withCredentials:是否携带发送提供凭据,在下面会进行详细的说明。 二、属性项 也就是指一些请求发送或完成过程中可能会被用户读取或使用到的一些值。 ① readyState:xhr的状态码。 ② status:http状态码。 ③ statusText:http状态说明文本。 ④ response:响应。 ⑤ responseType:响应类型。 ⑥ responseURL:响应的URL路径。 ⑦ responseText:响应的文本数据。 ⑧ responseXML:响应的XML数据。 ⑨ upload:上传对象,在下面会详细说明。 三、方法项 就是可以通过这些方法进行一些交互,如发送请求,获取或设置头部信息等等。 ① open:打开一个xhr请求。 ② setRequestHeader:设置请求头信息。 ③ send:发送请求。 ④ abort:中断请求。 ⑤ getResponseHeader:获取某一响应头内容。 ⑥ getAllResponseHeaders

Why indexOf in javascript not working?

a 夏天 提交于 2021-02-20 19:31:31
问题 I'm not sure what I'm doing wrong here. The first instance that I use indexOf it works perfectly fine, but when I use it the second time it's not returning the result that I'm expecting. function mutation(arr) { //return arr; res = ""; for (var x=0; x<arr[1].split("").length; x++) { if (arr[0].indexOf(arr[1].split("")[x]) !== -1) { res += "t"; } else { res += "f"; } } // res = ttt if (res.indexOf("f") !== -1) { return true; } else { return false; } } mutation(["hello", "hey"]); // this

HTML opacity attribute vs CSS opacity

♀尐吖头ヾ 提交于 2021-02-20 19:31:11
问题 I know of two different ways of setting opacity in HTML: <div opacity="0.5"></div> and <div style="opacity: 0.5;"></div> I also know how to set both of those in JavaScript: div.setAttribute("opacity", 0.5); and div.style.opacity = 0.5 Are there any major differences between those two methods? Should I prefer one over the other? (I guess I should at least be consistent) 回答1: The only opacity attribute I am aware of is for use with SVGs: Example Elements The following elements can use the

Run Nodemon with Typescript compiling?

可紊 提交于 2021-02-20 19:27:30
问题 I want my typescript files to be compiled on every file saving with the command tsc . How do I combine the tsc command with the command that nodemon runs in the build:live script "scripts": { "start": "npm run build:live", "build:live": "nodemon --watch '*.ts' --exec 'ts-node' app.ts", } this script causes nodemon to call itself twice or three times: "build:live": "nodemon --watch '*.ts' --exec 'ts-node app.ts & tsc'", 回答1: This looks like it will achieve what you're looking for: "start":

Can I use `obj.constructor === Array` to test if object is Array?

柔情痞子 提交于 2021-02-20 19:27:26
问题 Is it correct to use obj.constructor === Array to test if an object is an array as suggested here? Does it always returns correct answer compatible with Array.isArray ? 回答1: Depends, there are a few scenarios where it can return a different value, but Array.isArray will work. The Array object for one window is not the the same Array object in another window. var obj = someIframe.contentWindow.someArray; console.log(obj.constructor === Array);//false console.log(Array.isArray(obj));//true The

How to validate array length with io-ts?

杀马特。学长 韩版系。学妹 提交于 2021-02-20 19:26:27
问题 I am working on an io-ts validation where I would like to validate the list length (it has to be between min and max). I am wondering if there's a way to achieve this behavior since it could come quite handy at runtime for API endpoint validation. What I have so far is interface IMinMaxArray { readonly minMaxArray: unique symbol // use `unique symbol` here to ensure uniqueness across modules / packages } const minMaxArray = (min: number, max: number) => t.brand( t.array, (n: Array): n is t

How to cast observable response to local object

浪子不回头ぞ 提交于 2021-02-20 19:23:52
问题 In current Angular 6 application there is a subscribe to observable (response from RESTful service) this.activatedRoute.data.subscribe(({bag}) => { console.log(bag); this.bag= bag; }); the subscription is waiting response from resolver @Injectable({ providedIn: 'root' }) export class BagResolver implements Resolve<Bag> { constructor(private service: BagService) {} resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) { const id = route.params['id'] ? route.params['id'] : null; if

Geolocation in Safari 9 always returning Position Unavailable

荒凉一梦 提交于 2021-02-20 19:21:11
问题 I'm building an app where I need to get the user's location. To do this I have a click event attached to a button, which then calls navigator.geolocation.getCurrentPosition(getVenues, handleError); This works as expected on Chrome and Firefox, and the getVenues function is run after I grant access to my location. On Safari, after granting access the handleError function is immediately called. This happens on both my local and production server, both of which use HTTPS. My computer is on a

How to set specific timezone in datepicker using jquery only?

百般思念 提交于 2021-02-20 19:17:09
问题 I've a field and applying datepicker on it using jQuery. it is currently getting time from system/browser. I want it to get time from specific time zone e.g America/new_york. The endDate param is the key to set the calendar, means user should not be able to select the date from future. Currently it is looking like this The code snippet is : jQuery('#convo_start_date').datepicker({ format: 'dd M yyyy', endDate: '+0d', autoclose: true, showButtonPanel: true, todayBtn: 'linked' }).on('change',