Could somebody explain why the following simple script doesn't work in Firefox and how can I fix it?
<script type="text/javascript">
var w = window.open("a.php");
w.document.write("hello");
</script>
Thanks much
(edited to make the code sample display better)
DOM Scripting is more up to date and more robust.
e.g.
var w = window.open("a.php");
w.onload = function(){//you could also use dom ready rather than onload but that is a longer script that I've not included here
var body = w.document.getElementsByTagName("body")[0];
body.appendChild(document.createTextNode("bar"));
}
I'm pretty sure you can't read/ write files using javascript because it's a client side language? I may be completely wrong though!
EDIT:
Try this code:
var new_window, new_window_content = [];
new_window_content.push('<html><head><title>New Window</title></head>');
new_window_content.push('<body>New Window</body>');
new_window_content.push('</html>');
new_window = window.open("", "", "status,height=200,width=200");
new_window.document.write(new_window_content.join(''));
Cheers, Sean
To the others posting answers here: he is not trying to open a file and write to it - it is impossible in javascript in a browser. What he is instead trying to do with the w.document.write is to write to the end of the web page he has just opened in a browser.
Google Chrome says this code fails as:
>>> var w = window.open("http://www.google.co.uk");
undefined
>>> w.document.write("lol");
TypeError: Cannot call method 'write' of undefined
You first need to open the document stream by using document.open() - this will add a write function in w.document:
<script type="text/javascript">
var w = window.open("a.php");
w.document.open();
w.document.write("hello");
w.document.close();
</script>
Related
The powers that be have asked me to look into this and as much as I think it's not possible I want to be sure before going back to them. Don't ask me why they want this haha
They want to be able to use JavaScript that is defined in a variable. Let me explain...
Let's say you have this variable:
var testvar = "function dan() { alert('hello world'); }";
They want to be able to call dan() and have an alert popup in the web page. Something like this doesn't appear to be useable (of course).
var importjs = document.createElement('script');
importjs.src = testvar;
document.getElementsByTagName("head")[0].appendChild(importjs);
Any ideas or jargon I can use to explain why it's not doable. I believe it's essentially a cross origin issue. Our solution requires users to install software, which uses web sockets. This software performs a file GET for the JS and we then want to use the JS in the browser.
You should set the innerHTML of the script element to your String. The src of a script element should only be used to load an external script.
var testvar = "function dan() { alert('hello world'); }";
var importjs = document.createElement('script');
importjs.innerHTML = testvar;
document.getElementsByTagName("head")[0].appendChild(importjs);
dan();
I am a designer and am producing an html5 banner for my client using Edge. The adserver they are using requests that this simple javascript be inserted into the html to pass some tracking info. But Dreamweaver says there is a syntax error whenever i add the js. Here it is:
<script type="text/javascript">
var clickTAG = "<!mpvc/>http://<!mpck/>";
document.location = clickTAG;
</script>
Can anyone tell me what the syntax error is? Thank you!
Looks like that those semicolons aren't real semicolons, they're Greek Question Marks (hex 0x037e). Change them to normal semicolons, and it should run fine.
var clickTAG = "<!mpvc/>http://<!mpck/>";
document.location = clickTAG;
Although this question is similar, it is not what I am looking for.
Let's say on HostA.com I include a script from HostB.com:
<script type="text/javascript" src="http://www.hostb.com/script.js">
When script.js runs, I need to get the name of HostB (let's assume it can change). If I use:
var hostName = window.location.hostname;
It will return HostA.com rather than HostB.com obviously because that is where the the window object is scoped.
How can I get the name of HostB from within the script? Do I have to locate the <script> element in the DOM and parse the src attribute or is there a better way?
EDIT
Yes, it is on my server, but may be on other servers as well. I am developing a javascript plugin and am trying to make absolute paths so it doesn't try to reference files on the server including the plugin.
Here is how: first off, include this as the first line of your script. I know it is a comment. Do it anyways
//BLAHBLAHBLAHBLAHAAABBBCCCDDDEEEFFFGGGILIKEPI
next, use this function inside of that script to determine the host
function findHost(){
var scripts=document.getElementsByTagName('script');
var thisScript=null;
for(var i=0;i<scripts.length;i++){
if(scripts[i].innerHTML.indexOf('//BLAHBLAHBLAHBLAHAAABBBCCCDDDEEEFFFGGGILIKEPI')!==-1)
var thisScript=scripts[i];
}
var urlParser=document.createElement('a');
urlParser.href=thisScript.getAttribute('src');
return urlParser.hostname;
}
I am loading the script with RequireJS which looks something like this:
<script data-main="http://hostb.com/js/app/main.js" src="http://hostb.com/js/vendor/require.js" type="text/javascript"></script>
I figured out, with help from #adeneo that I can do something like this:
$('script[data-main*="/js/app/main.js"]').attr('data-main')
Which returns:
http://hostb.com/js/app/main.js
And I can parse it for the hostname.
var url = $('script[data-main*="/main.js"]').attr('data-main');
parser = document.createElement('a');
parser.href = url;
host = parser.hostname;
Thanks for the suggestions and nudge in the right direction!
BREAKING NEWS
Turns out their is an easier way for anyone using RequireJS (who finds this question in search) and needs to be able to load absolute URL's with the script host:
var myCssPath = require.toUrl('css/mystyles.css');
That builds an absolute path using the hostname of the server running!
To omit using the hostname twice (as you described in your 'accepted answer') I implemented the solution this as follows:
HTML on HostA.com:
<script data-main="my_embed_id" src="http://hostb.com/js/vendor/require.js" type="text/javascript"></script>
require.js on HostB.com:
// get host where this javascript runs
var url = $('script[data-main="my_embed_id"]').attr('src');
var hostb = url.replace(/(\/\/.*?\/).*/g, '$1');
Which returns:
http://hostb.com
Inspired by: How to make an external javascript file knows its own host?
I am loading a script using the script embed tag and I want to get the url variables and the loading script name inside the script. Is it possible? For Example,
<script src="test.js?id=12"></script>
Inside test.js is there any way to get the url variable id?
Any help would be appreciated.
Thanks,
Karthik
Aside from the answers in the linked post, FWIW with Firefox 4 only you can (with caveats); document.currentScript.src which will return the full url, including arguments.
Thanks for all your efforts I have made that working by assigning an id attribute in the script tag and accessed via jQuery,
<script src="test.js?id=12" id="myScript"></script>
var currentScript = $("#myScript").attr('src'); //This will give me my script src
Thanks,
Karthik
If you want to get a variable from the current URL you can use this:
function queryParser(url){
this.get=function(p){return this.q[p];}
this.q={};
this.map=function(url){
url=url || window.location.search.substring(1);
var url=url.split('&');
var part;
for(var i=0;i<url.length;i++){
part=url[i].split('=');
this.q[part[0]]=part[1];
}
}
this.map(url);
}
var query=new queryParser();
// assuming you have ?test=something
alert(query.get('test'));
I recommend you map the result, so you don't re-parse whenever you want to find a specific element.
I don't really know why you'd pass a query string in a script tag like that, unless you specifically want off-site includes with a simple robust system for various effects. Or are actually using PHP to handle that request.
If you want to "send" a variable to one of your scripts, you can always do:
<script type="text/javascript">
var myVar="I'm in global scope, all scripts can access me";
</script>
<script src="test.js?id=12"></script>
If you really need to get the URL of the currently included script, you can use the code supplied by my peers in the other answers, you can then use:
var query=new queryParser(scriptURL);
alert(query.get('id'));// would alert 12 in your case
Navigating through the link on your comments you can get the proper answer.
Anyway, to make things easier:
var allScripts=document.getElementsByTagName('script');
var indexLastScript= allScripts.length -1;
alert (allScripts[indexLastScript].src);
This will show up "test.js?id=12" as a regular String so its up to you to split it in order to get de param.
Hope it helps, I've tried it on the run over the Chrome Javascript Console. :D.
I'd like to create a handle through which I would be able to inject javascript to Firefox.
I can do it very easily with IE but Firefox is a different story...
Can anyone help?
You can use the grease monkey extension.
What do you want to do exactly:
insert JS in your own browser manually?
insert JS in your browser automatically for some websites?
insert JS in somebody else browser when they visit your website?
Maybe you can describe how you do it with IE. Once we know what you are trying to achieve, we'll probably be able to answer you.
That's how I do it in a manual way, using Firebug extension.
Console.
Find a small triangle on the right that's "Command editor", press it.
Paste the code.
Run
The code:
var b = document.createElement("div");
b.innerHTML = "<button onclick='jarektest();'>text</button>";
document.body.appendChild(b);
// short code typed inline
var s = document.createElement("script");
s.innerHTML = "function jarektest() { alert(document.body.innerHTML); }";
document.body.appendChild(s);
// longer piece, taken from other file
var s2 = document.createElement("script");
s2.src = "file:///J:/lang/js/hello.js";
document.body.appendChild(s2);