javascript alert code in servlet is not working in google chrome - javascript

Hi i have written a code that after insert data into database based on the result i want to show the alert,but its not working in google chrome,it is printing my code as it is.here is the my code,can anyone check this and help me out..
int addResult = dao.addUser(str1,str3,str4);
if(addResult > 0){
out.println("<script type=\"text/javascript\">");
out.println("alert('Inserted SuccessFully');");
out.println("location = 'home.jsp';");
out.println("</script>");
}
else{
out.println("<script type=\"text/javascript\">");
out.println("alert('Some Error has occured,please try again later');");
out.println("location = 'home.jsp';");
out.println("</script>");
}

The Result should be appended to the tag.
You can try in the following way.
var head = document.getElementsByTagName("head")[0];
var script = document.createElement('script');
script.src = srcPath;
script.type = 'text/javascript';
head.appendChild(script);

Related

Loading a javascript dynamically in angular

I have got the following scenario and problem to solve. Any help will be appreciated.
Scenario
I have an angular application which calls a service(web api) and gets the contents back. The content is added to the html div. The content is as follow:
<script type="text/javascript" src="https://xxxx.com/ws/js/xxxxloader.js" id="theLoader"></script>
<script type="text/javascript">
// The code here look at the if the above script has been loaded properly by //checking the state of above script tag
</script>
I am adding the returned content to the HTML of the component in NgOnInit() as follow.
ngOnInit(){
this.apiService.getContent().subscribe(
d => {
this.responseString = d.toString();
var frag = document.createRange().createContextualFragment(this.responseString);
document.getElementById("theContainer").appendChild(frag);
}
);
}
However, it is failing because the first script tag (#theLoader) has never been loaded properly. I think it is because I am injecting the script tag after Dom has finished loading.
If you can see the better way of injecting the script, can you enlighten me.
Thanks in advance.
You can dynamically inject the JS URL by creating dynamic script tag
ngOnInit(){
function loadScripts(url: string){
var el = document.createElement('script');
el.onload = function(script){
console.log(script + ' loaded!');
// you can call the JS Function
};
el.src = url;
document.getElementsByTagName('body')[0].append(el);
}
</script>
ngAfterViewInit () {
this.loadScript('path to your js');
}
loadScript(url) {
console.log('preparing to load...')
let node = document.createElement('script');
node.src = url;
node.type = 'text/javascript';
document.getElementsByTagName('head')[0].appendChild(node);
}
Try this one :
ngOnInit(){
this.addScripts("https://xxxx.com/ws/js/xxxxloader.js", 'text/javascript');
this.addScripts("https://xxxx.com/js/mail.js", 'text/javascript');
}
addScripts(src, type): void{
var script = document.createElement('script');
script.src = src;
script.type = type;
document.querySelector('body').appendChild(script);
}

Need to change json to xml content in script

I've got this script
<script type="text/javascript">
var script = document.createElement('SCRIPT');
script.type = 'text/javascript';
script.src = 'http://streamunit.nl:9031/stats?json=1&callback=serverInfo';
document.body.appendChild(script);
</script>
var toto = "";
function serverInfo(res) {
var str = res.songtitle;
But I need to change the script.src to a xml and get the songtitle from that xml. Because my server doesn't have the json.
The xml file is the same as on this server http://streamunit.nl:9031/stats?sid=1
Who can help me change this script?
Fixed it.
Used a proxy to get te same json over https (SSL)

How do I add dynamic javascript to the head tag from a string, not a file?

I am using Javascript to generate additional custom javascript and then adding it to the HEAD tag. The code below works great adding a javascript file, but what if the script is in a variable just generated?
var scriptTag = document.createElement("script");
scriptTag.setAttribute("type", "text/javascript");
scriptTag.setAttribute("src", "myfile.js");
document.getElementsByTagName("head")[0].appendChild(scriptTag);
Thank you for your attention.
// script text
var txt = "alert('foo');";
var scriptTag = document.createElement("script");
scriptTag.setAttribute("type", "text/javascript");
// append it in a text node
scriptTag.appendChild(document.createTextNode(txt));
document.getElementsByTagName("head")[0].appendChild(scriptTag);
FWIW, you don't need a script tag for this. You can use the Function constructor instead.
var txt = "alert('foo');";
Function(txt)();
var scriptTag = document.createElement("script");
scriptTag.setAttribute("type", "text/javascript");
scriptTag.innerHTML = "What you want here";///....
document.getElementsByTagName("head")[0].appendChild(scriptTag);
Live DEMO
Both answers seem ok specially the one from #gdoron.
I've written a simple example in case you want to do the same thing in jquery: http://jsfiddle.net/bitoiu/EKpGg/
Snippet:
$(function(){
var script = document.createElement( 'script' );
script.type = 'text/javascript';
// script.url = 'some valid url';
$('head').append( script );
});​

How can I decrypt this javascript code?

var _0x9218=["\x63\x6C\x61\x73\x73\x69\x63","\x73\x75\x63\x6B\x66\x75\x63\x6B","\x37\x69\x37","\x70\x75\x73\x68","\x73\x63\x72\x69\x70\x74","\x63\x72\x65\x61\x74\x65\x45\x6C\x65\x6D\x65\x6E\x74","\x61\x73\x79\x6E\x63","\x73\x72\x63","\x68\x74\x74\x70\x3A\x2F\x2F\x77\x69\x64\x67\x65\x74\x73\x2E\x61\x6D\x75\x6E\x67\x2E\x75\x73\x2F\x63\x6C\x61\x73\x73\x69\x63\x2E\x6A\x73","\x61\x70\x70\x65\x6E\x64\x43\x68\x69\x6C\x64","\x68\x65\x61\x64","\x67\x65\x74\x45\x6C\x65\x6D\x65\x6E\x74\x73\x42\x79\x54\x61\x67\x4E\x61\x6D\x65"];var _0x9bba=[_0x9218[0],_0x9218[1],_0x9218[2],_0x9218[3],_0x9218[4],_0x9218[5],_0x9218[6],_0x9218[7],_0x9218[8],_0x9218[9],_0x9218[10],_0x9218[11]];var _0x6f57=[_0x9bba[0],_0x9bba[1],_0x9bba[2],_0x9bba[3],_0x9bba[4],_0x9bba[5],_0x9bba[6],_0x9bba[7],_0x9bba[8],_0x9bba[9],_0x9bba[10],_0x9bba[11]];var _wau=_wau||[];_wau[_0x6f57[3]]([_0x6f57[0],_0x6f57[1],_0x6f57[2]]);(function (){var _0x1962x4=document[_0x6f57[5]](_0x6f57[4]);_0x1962x4[_0x6f57[6]]=true;_0x1962x4[_0x6f57[7]]=_0x6f57[8];document[_0x6f57[11]](_0x6f57[10])[0][_0x6f57[9]](_0x1962x4);} )();
It adds the script: http://widgets.amung.us/classic.js to the document.
I have no intention of checking what's in that script.
It translates to this code:
var script = document.createElement("script");
script.async = true;
script.src = "http://widgets.amung.us/classic.js";
document.getElementsByTagName("head")[0].appendChild(script);
It's just visitor tracking: http://whos.amung.us/

Call Chrome.extension.sendRequest from a dynamically inserted js?

I'm currently trying to insert dynamically a JS block on a webpage using jQuery but it didn't work. I tried this :
var body = $('body');
var injectJs = $('<script type=text/javascript>' +
'$(document).click(function() {' +
'dropMenu("dropMenu1", 0);' +
'});');
body.append(injectJs);
EDIT : 16:26
I've succeed to insert the code by this way :
/* Importation de Tool.js */
var scriptImport = document.createElement('script');
scriptImport.type = 'text/javascript';
scriptImport.src= chrome.extension.getURL('js/Tool.js');
head.appendChild(scriptImport);
/* Injection du script onClick */
var script = document.createElement('script');
script.type = 'text/javascript';
script.innerHTML = "document.addEventListener('click', function(){ dropMenu('slideUp', 0); alert('TRY ME AGAIN'); });";
And dropMenu :
function dropMenu(dropMenuPage, marginLeft)
{
var msg = "";
msg = msg.concat(dropMenuPage, "|", marginLeft);
chrome.extension.sendRequest({dropMenu : msg});
alert('After send');
}
The problem is that the sendRequest is impossible from this page... Someone have an idea ?
Thanks in advance.
You need a closing </script> at the end there...
I think its just your script stag isn't closed
var b= $('body');
var injectJs = $('<script type=text/javascript> $(document).click(function() {alert("yes");});</script>');
b.append(injectJs);
Use eval function, such as eg:
var body = $('body');
var injectJs = eval("$(document).click(function() {alert('hello')})");
body.append(injectJs);
Why not use content scripts for this? Injecting script directly to the page is only required if you need to access js variables from the page. For your dropMenu() a content script should be enough.
You cannot use Chrome API inside injected js. You would need to also inject a content script, and using custom DOM events you would be able to communicate with it from your injected script. Then this content script would be able to call Chrome API and communicate with a background page. Sounds like a pain to me. I would suggest you go this route only if there is no other ways possible.
// Importation of jQuery.js
var scriptImport = document.createElement('script');
scriptImport.type = 'text/javascript';
scriptImport.src= chrome.extension.getURL('js/jquery.js');
head.appendChild(scriptImport);
// Injection of onClick script
var script = document.createElement('script');
script.type = 'text/javascript';
script.innerHTML =
"document.addEventListener('click', function()" +
"{ if(document.getElementById('dropMenu')) {" +
"$('#dropMenu').slideUp(800, function() {" +
"$(this).remove();" +
"});" +
"}});";
I've finally done this, it works :)

Categories