Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript设置iframe src-正确的格式_Javascript_Html_Urlencode_Ampersand - Fatal编程技术网

Javascript设置iframe src-正确的格式

Javascript设置iframe src-正确的格式,javascript,html,urlencode,ampersand,Javascript,Html,Urlencode,Ampersand,我已经阅读了帖子和,但是没有正确的编码 在javascript中,我编写了一个指向的链接,然后使用document.getElementById('downloadlink').value读取该链接,并将其放入要执行的iframe src中 该代码在我的本地主机上运行正常,但在live server上运行不正常,在live server中,由于名称无效,文件从未加载(注意:http 302临时移动是由于无效的文件请求导致.htaccess重定向)。我确信这是编码符号的问题。我的理解是,输入需要对

我已经阅读了帖子和,但是没有正确的编码

在javascript中,我编写了一个指向
的链接,然后使用document.getElementById('downloadlink').value读取该链接,并将其放入要执行的iframe src中

该代码在我的本地主机上运行正常,但在live server上运行不正常,在live server中,由于名称无效,文件从未加载(注意:http 302临时移动是由于无效的文件请求导致.htaccess重定向)。我确信这是编码符号的问题。我的理解是,
输入
需要对符号(和其他html实体)进行编码。为此,我使用了php.js的等价物
htmlspecialchars()
htmlspecialchars\u decode()
(这同样适用于localhost)

对于
img.src
,我的理解是我希望将安培数编码为
&
但是在我的本地主机上,它可以在没有它们的情况下工作,但不能在编码它们的情况下工作。在我的live站点上,这两种方式都不起作用

为了对它们进行编码,我尝试了:

url = url.replace(/&/g, "&"); 
是时候停止扯我的头发,寻求帮助了。有人吗

iframe(使用jshtmlspecialchars编码)注意:我无法获取
&为显示保留编码-保存时会被替换。

<iframe src="www.waldorfteacherresources.com/getfile.php?file=g2-saints-martin-009.jpg&amp;mode=download&amp;hv=4443f86959bf104e1df0eac204b8aaf226ae533b&amp;wtrpath=docs " id="iframe" height="0" width="0" hidden=""></iframe>

代码

function downloadfile() {
    if (document.getElementById("downloadlink")) {
    var div = document.getElementById('datadiv');
    var url = hx(document.getElementById('downloadlink').value);
    var ifrm = document.createElement("iframe");

    ifrm.setAttribute("src", url);
    ifrm.setAttribute("id", "iframe");
    ifrm.height = 0;
    ifrm.width = 0;
    ifrm.hidden = true;
    div.appendChild(ifrm);
    }
}

// support functions to encode / decode 

// encodes output - equivalent of php hx function
// hx notation is a shortcut for htmlspecialchars() with all options set
function hx( string,  flags,  charsetEncoding,  double_encode) {
    if (typeof flags == "undefined"){
        flags = 0;
    }
    if (typeof charsetEncoding == "undefined" ){
    charsetEncoding = "UTF-8";
    }    
    if (typeof double_encode == "undefined"){
        double_encode = true;
    }

    // constants not valid until php v 5.4
    var ENT_HTML401 = 0;
    var ENT_HTML5 = (16 | 32);
    var ENT_COMPAT = 2;
    if ( flags == 0) {
     flags = ENT_COMPAT |  ENT_HTML401;
    }
     string = htmlspecialchars( string,  flags,  charsetEncoding,  double_encode);
    return  string;
}

// decodes output of hx() / htmlspecialchars() - shortcut notation for htmlspecialchars_decode()
function hdx(string) {
    return htmlspecialchars_decode(string);
}

function htmlspecialchars(string, quote_style, charset, double_encode) {
  //       discuss at: http://phpjs.org/functions/htmlspecialchars/
  var optTemp = 0,
    i = 0,
    noquotes = false;
  if (typeof quote_style === 'undefined' || quote_style === null) {
    quote_style = 2;
  }
  string = string.toString();
  if (double_encode !== false) { // Put this first to avoid double-encoding
    string = string.replace(/&/g, '&amp;');
  }
  string = string.replace(/</g, '&lt;')
    .replace(/>/g, '&gt;');

  var OPTS = {
    'ENT_NOQUOTES': 0,
    'ENT_HTML_QUOTE_SINGLE': 1,
    'ENT_HTML_QUOTE_DOUBLE': 2,
    'ENT_COMPAT': 2,
    'ENT_QUOTES': 3,
    'ENT_IGNORE': 4
  };
  if (quote_style === 0) {
    noquotes = true;
  }
  if (typeof quote_style !== 'number') { // Allow for a single string or an array of string flags
    quote_style = [].concat(quote_style);
    for (i = 0; i < quote_style.length; i++) {
      // Resolve string input to bitwise e.g. 'ENT_IGNORE' becomes 4
      if (OPTS[quote_style[i]] === 0) {
        noquotes = true;
      } else if (OPTS[quote_style[i]]) {
        optTemp = optTemp | OPTS[quote_style[i]];
      }
    }
    quote_style = optTemp;
  }
  if (quote_style & OPTS.ENT_HTML_QUOTE_SINGLE) {
    string = string.replace(/'/g, '&#039;');
  }
  if (!noquotes) {
    string = string.replace(/"/g, '&quot;');
  }

  return string;
}

function htmlspecialchars_decode(string, quote_style) {
    //       discuss at: http://phpjs.org/functions/htmlspecialchars_decode/
    var optTemp = 0,
        i = 0,
        noquotes = false;
    if (typeof quote_style === 'undefined') {
    quote_style = 2;
    }
    string = string.toString().replace(/&lt;/g, '<').replace(/&gt;/g, '>');
    var OPTS = {
    'ENT_NOQUOTES': 0,
    'ENT_HTML_QUOTE_SINGLE': 1,
    'ENT_HTML_QUOTE_DOUBLE': 2,
    'ENT_COMPAT': 2,
    'ENT_QUOTES': 3,
    'ENT_IGNORE': 4
    };
    if (quote_style === 0) {
    noquotes = true;
    }
    if (typeof quote_style !== 'number') { // Allow for a single string or an array of string flags
    quote_style = [].concat(quote_style);
    for (i = 0; i < quote_style.length; i++) {
        // Resolve string input to bitwise e.g. 'PATHINFO_EXTENSION' becomes 4
        if (OPTS[quote_style[i]] === 0) {
        noquotes = true;
        } else if (OPTS[quote_style[i]]) {
        optTemp = optTemp | OPTS[quote_style[i]];
        }
    }
    quote_style = optTemp;
    }
    if (quote_style & OPTS.ENT_HTML_QUOTE_SINGLE) {
    string = string.replace(/&#039;/g, "'");
    }
    if (!noquotes) {
    string = string.replace(/&quot;/g, '"');
    }
    // Put this in last place to avoid escape being double-decoded
    string = string.replace(/&amp;/g, '&');

    return string;
}


The request headers

    GET /www.example.com/getfile.php?file=myfile.jpg&mode=download&hv=939afca0cdaafd55a1e1471da7463be9acbf5478&wtrpath=docs HTTP/1.1
    Host: www.example.com
    Connection: keep-alive
    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
    Upgrade-Insecure-Requests: 1
    User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36
    Referer: http://www.waldorfteacherresources.com/index.php?grade=2&page=Saints
    Accept-Encoding: gzip, deflate, sdch
    Accept-Language: en-US,en;q=0.8
    Cookie: id=XXX;
    PHPSESSID=.... session info here

The response

    HTTP/1.1 302 Moved Temporarily
    Date: Sun, 21 Feb 2016 21:16:05 GMT
    Server: Apache
    X-Powered-By: PHP/5.5.32
    **** this is an .htaccess redirect due to an invalid file request
    Location: /index.php
    Cache-Control: max-age=86400
    Expires: Thu, 01 Jan 1970 00:00:00 GMT
    Vary: Accept-Encoding
    Content-Encoding: gzip
    Content-Length: 767
    Keep-Alive: timeout=3, max=100
    Connection: Keep-Alive
    Content-Type: text/html; charset=UTF-8
函数下载文件(){
if(document.getElementById(“downloadlink”)){
var div=document.getElementById('datadiv');
var url=hx(document.getElementById('downloadlink').value);
var ifrm=document.createElement(“iframe”);
setAttribute(“src”,url);
ifrm.setAttribute(“id”、“iframe”);
ifrm.height=0;
ifrm.width=0;
ifrm.hidden=true;
儿童分部(ifrm);
}
}
//支持编码/解码功能
//编码输出-相当于php hx函数
//hx表示法是设置了所有选项的htmlspecialchars()的快捷方式
函数hx(字符串、标志、字符集编码、双_编码){
如果(标记类型==“未定义”){
flags=0;
}
if(字符集编码的类型==“未定义”){
charsetEncoding=“UTF-8”;
}    
if(双_编码类型==“未定义”){
双_编码=真;
}
//在PHPV5.4之前,常量无效
var ENT_HTML401=0;
变量ENT_HTML5=(16 | 32);
变量t_COMPAT=2;
如果(标志==0){
flags=ENT|u COMPAT | ENT|u HTML401;
}
string=htmlspecialchars(字符串、标志、字符集编码、双_编码);
返回字符串;
}
//解码hx()/htmlspecialchars()的输出-htmlspecialchars_decode()的快捷表示法
函数hdx(字符串){
返回htmlspecialchars\u解码(字符串);
}
函数htmlspecialchars(字符串、引号样式、字符集、双字符编码){
//讨论地点:http://phpjs.org/functions/htmlspecialchars/
var optTemp=0,
i=0,
noquotes=false;
if(typeof quote_style=='undefined'| | quote_style==null){
quote_style=2;
}
string=string.toString();
如果(double_encode!==false){//将此项放在第一位以避免双重编码
string=string.replace(/&/g,,&;');
}
string=string.replace(//g',);
变量选项={
“ENT_NOQUOTES”:0,
'ENT_HTML_QUOTE_SINGLE':1,
'ENT_HTML_QUOTE_DOUBLE':2,
"台胞":二,,
“ENT_引号”:3,
“entu忽略”:4
};
如果(引号_样式===0){
noquotes=true;
}
if(typeof quote_style!=='number'){//允许单个字符串或字符串标志数组
quote_style=[].concat(quote_style);
对于(i=0;i