benhowdle89
benhowdle89

Reputation: 37504

Set iFrame source to "view-source: {URL}" not working

I'm trying to set the src of an iframe to "view-source: {URL}" using the following:

var url = document.getElementById('url');
var submit = document.getElementById('submit');
var target = document.getElementById('target');
submit.onclick = function(){
    window.frames['target'].location.href = "view-source:" + url.value; 
};

But it seems to set the src of the iframe relative to my website where the iframe is. I suppose because "view-source: x" only works when implemented with "location.href"?

Is this the case?

I'm using JS by the way!

Html:

<input type="text" id="url"></input>
<button id="submit">Submit</button>

<iframe id="target" src=""></iframe>

Upvotes: 1

Views: 1031

Answers (1)

Dagg Nabbit
Dagg Nabbit

Reputation: 76766

This is pretty interesting.

There must be some kind of restriction on "view-source" URLs in windows that already exist, because opening a new window via window.open works, but opening an existing window (that happens to be an iframe) with the "view-source" URL doesn't work, and opening the URL in a top level window that is already present also doesn't work (try the last example twice with different URLs, it will only work the first time).

view-source is probably best treated as browser black magic voodoo and not as a regular URI scheme.

All of this was tested in Chrome, by the way. The rules may be different for other browsers supporting "view-source."

Upvotes: 1

Related Questions