最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to prevent the symbol "&" from being replaced by "&" -

programmeradmin4浏览0评论

I am looking to prevent the symbol "&" from being replaced by "&" within my URL, specifically within JavaScript.

Just to expand on this requirement, I am getting my url from an oracle database table, which I then use within Oracle Application Express, to set the src attribute of an iframe to this url.

FYI, the url stored in the Oracle table is actually stored correctly, i.e.

;f=mydir/Summary.xml

What appears in my use when trying to pass over into iframe src using JavaScript is:

;amp;f=mydir/Summary.xml

which basically returns a page cannot be found

I am looking to prevent the symbol "&" from being replaced by "&" within my URL, specifically within JavaScript.

Just to expand on this requirement, I am getting my url from an oracle database table, which I then use within Oracle Application Express, to set the src attribute of an iframe to this url.

FYI, the url stored in the Oracle table is actually stored correctly, i.e.

http://example./xml/getInfo?s=pvalue1&f=mydir/Summary.xml

What appears in my use when trying to pass over into iframe src using JavaScript is:

http://example./xml/getInfo?s=pvalue1&f=mydir/Summary.xml

which basically returns a page cannot be found

Share Improve this question edited Nov 18, 2019 at 20:53 halfer 20.4k19 gold badges108 silver badges201 bronze badges asked Jun 8, 2010 at 13:43 tonyftonyf 35.6k53 gold badges165 silver badges260 bronze badges 10
  • What does your URL e from? – SLaks Commented Jun 8, 2010 at 13:44
  • um.. do you mean prevent & from being replaced by & when it's sent through? – Dan Heberden Commented Jun 8, 2010 at 13:44
  • Can you provide some of the surrounding code to illustrate the problem? It's probably the case that your server-side code is "escaping" the ampersand, which would generally not be what you'd want for Javascript. However, we don't know what your server-side language is, so it's hard to say how to fix it. – Pointy Commented Jun 8, 2010 at 13:45
  • You get a URL from somewhere using some method (and possibly you put the URL somewhere (which could be the same somewhere or somewhere else) using some other method) but you don't tell us where the somewheres are or what the methods are. It isn't possible to answer this without making some huge assumptions. – Quentin Commented Jun 8, 2010 at 13:46
  • when encoding & into url in html you should always use &amp; instead of &, e.g. <img src="http://site.cz/?a=0&b=1" /> should be indeed written as site.cz/?a=0&amp;b=1 (right?), so someone automatically does that conversion (who? where?... we can't know); I think %26 can be used if you want to "pass" a literal &, e..g site.cz/?a=0%26b=1 "assigns" to a 0&b=1 – ShinTakezou Commented Jun 8, 2010 at 13:53
 |  Show 5 more ments

2 Answers 2

Reset to default 8

I suspect you are doing something like this:

1) Selecting the URL text from the database into an Apex page item.

2) In Javascript, getting the URL text from the page item and using it to set the iframe source.

When you select the value in step 1, Apex will automatically replace the "&" by "&" so that the page HTML is valid - it will be something like:

<input type="hidden" id="P1_URL" 
 value="http://mydomain./xml/getInfo?s=pvalue1&amp;f=mydir/Summary.xml" />

You will therefore have to reverse the transformation in your Javascript code - something like:

document.getElementById('myIframe').src = $v('P1_URL').replace('&amp;','&');

I had the same problem and it caused me a lot of grief using PHP and JS.

This was how I solved the problem, using substring(0,1).

function callAJAX()
    {      
        var var_Date = document.getElementById('DateAJAX').value;    
        var l2 = '&type=' + <?php echo $type; ?>; 
        var l2=l2.substring(0,1) + '<?php echo "type="  . $type; ?>';  // stupid js fix 
        jsfunction('ajaxdiv', 'phpfilename.php?date=' +var_Date + l2,'<? echo $num; ?>'); 
    }

The URL will result in something like:

phpfilename.php?date=2012-10-31&type=2

Yes, it's a long example, but I'm sure you can extract all the extra junk to be a minimalist example. :)

发布评论

评论列表(0)

  1. 暂无评论