Insert javascript alert into url [duplicate] - javascript

This question already has answers here:
Call Javascript function from URL/address bar
(14 answers)
Closed 2 years ago.
I am currently learning some web programming related stuff. I'm a little confused on how I insert some javascript into say a random URL. I'm trying to insert an alert message with an echo payload so I couldn't do javascript:alert("testtestesttest"); which will work.
Should this not work?
echo?payload=javascript:alert("test");
I am using an older version of my browser so that javascript execution is possible. But for some reason javascript:alert("test"); works by itself but when i add it onto the end of the url with the echo payload it's just echoing the text after the payload.
EDIT: I have found my solution. Sorry.

you can do it ) of course everything is possible! Not sure modern browsers will allow this request. Need to check.
Buth this approach is usafe from the user prospective.
Imagine that somewone will put tricky code and use your site domain as trusted and will send a spam using this link.
Hey bro here is discount you can get!
https://someknownSite.com?javascript::getyourpasswordcode

Then why do you want to make this happen via URL? Let say you have a page, called "myAlert.php".
So what you can do is, you can write a script on this page, which will simply show the alert when somebody with this URL will access this.
Or you can also, write a simple condition that when a request has been made to this URL, you can show the alert box.
But If you are trying to insert the javascript in the URL and expecting the result then it's not possible. Because modern browsers won't let you run a script as the "< >" symbols will be changed.

Related

How to get a variable from php to JS and back [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 6 years ago.
First of all, I'm relatively new to this so I'm sorry if I word stuff weird.
So let's say I have the variable $x with the value 1 in PHP. I want to get the variable to my JS-Script and modify it and then send the variable x with the modified value back to my PHP document. Note I don't want to send it to a server and I don't want to use AJAX (have yet to learn it). If there's no other way It'd be nice if someone could show me an example of how to do this with AJAX.
You should use AJAX if possible it is more comfortable and with jQuery not hard to use/understand. But if this topic is new for you, you can try to send it via a window.location.href.
I looked for some good examples for you:
PHP to JS
Without AJAX
Video - JS to PHP with jQuery POST
To save PHP variables from PHP to Javascript you often just print them. If you're more familar with Javascript and PHP, you should try to work with JSON-Strings. But I think that would be to much for your first try.
Hope that helps.

Is it possible to continuing running JavaScript code after a redirect? [duplicate]

This question already has answers here:
execute a function after redirecting - javascript
(3 answers)
Closed 7 years ago.
So, on my website, a user types in a subject they want the gist of, then after searching, it redirects them to a Wikipedia API displaying the general idea of the subject. However, there's a bunch of information about the API that gets in the way on the webpage, so I need to use JavaScript to get rid of that excess stuff.
Unfortunately, after changing webpages, it seems I can't run any more code from my website.
Any solution to this?
You could use an iframe, like so:
<iframe id="wikipedia-stuff">
</iframe>
And populate it like so:
<script>
document.getElementById("wikipedia-stuff").src = "http://wikipedia.stuff";
</script>
The wikipedia stuff would be in the iframe, and your code would still be running.

Need to Understand Process behind Web Processes with HTML, PHP, Javascript, through existing HTML Button to PHP function Issue [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Note: I'm asking less for a solution and more for help. I understand this question is probably basic and repetitive, but I'm less sorry than I am frustrated with how little other websites explain.
I am absolutely positive this question has been asked in a million variations because I've looked it up so many times, but the problem is how many times I've had to look it up and I still don't understand half of what I'm doing every time I attempt it. Pretty sure I've scraped the barrel with this one.
I have a process I'm running that calls several queries to a SQL database, and I'm doing them in PHP. Problem is, they're all run automatically when the page is opened, and that makes me so queasy it isn't funny. I'm trying to make a button that does not redirect to another page to call this process, be it by function or what-have-you. I'm trying to keep all of this on one page... Though I'm not averse to make it more pages.
I'm trying to avoid JQuery. My tools available are: JavaScript, PHP, and HTML. If I need to, I'm willing to convert all 8 or 9 queries into JavaScript to run it through button or even AJAX, but I'd like to keep it in PHP if I can. I know PHP and HTML run differently, but this is where I get confused on how best to combine them, and how to use JavaScript, and when to use JavaScript, and what GET and POST and SUBMIT are in relation to PHP, and just a million things. I'd like to know the BEST method AND the "you can do this with what you have already done", and WHY one is better than the other, if at all possible.
I'm sure 90% of this question is super repeated, but thank you in advance for your patience. I hate not understanding.
EDIT: I was asked for my code, and I can give a basic idea of it, without being able to give the code itself:
<?php
mysql_pconnect ('host', 'username', 'password');
mysql_select_db('database');
// the company I work for has so far refused to let me upgrade our website, so I
// am trying to keep it the same so that half of it isn't new and the other
// super outdated. Believe me, I'm pushing.
$query0 = "TRUNCATE table templateTable;";
$query1 = "INSERT INTO templateTable
(item1, item2, item3, item4...)
SELECT
table1.itemA, table2.itemB, table1.itemC, table2.itemD...
FROM table1, table2
WHERE table1.itemA = table2.itemQ;";
// there are a LOT of items in this query, like 100
$query2 = "UPDATE templateTable, table1
SET templateTable.itemX = \"thing\",
CASE
WHEN table1.itemAB = 1
THEN itemX = 'THING1';";
$query3 = "UPDATE templateTable, table2, table1
SET templateTable.itemY =
CASE
WHEN table2.modelNumber = table1.modelNumber
THEN itemY = table2.modelNumber
ELSE
itemY = table1.otherInformation;";
// these queries are using data from two tables to fill an inventory
// template table that will only be filled and edited through queries. there
// is no manual editing of this table- just queries.
/*
I'm hoping you get the gist of this because there's 5 other queries even
longer and more complicated #.# I have a procedure in MySQL to do this but my
coworkers are nervous about running the procedure, so I'm trying to make it more
friendly by allowing it through the website. This was requested of me
*/
?>
Long story short: I'm trying to run a PHP function on the same page with HTML through a button, the php has 8 queries being run to a MySQL database, and I don't really understand any of this.
I'm trying to run a PHP function [...] through a button [...] that
does not redirect [or reload]
First you need to understand when is PHP executed, when is HTML rendered and when is Javascript executed.
Look for "difference between cliend-side and server-side" on the Internet, you'll find good explanations. It seems to be a big deal when you're a beginner but once you understand that, knowing when you have to use PHP, if and when you have to use Javascript, and which is the most accurate, is quite easy to get.
In a few words :
PHP is a server-side language. When you load a webpage, your browser (client) sends a request to the server. The server executes the PHP code, generates some HTML (in our case), and sends this HTML back to the browser.
HTML is rendered client-side. This is your browser that reads and renders HTML.
Javascript is a client-side language. Javascript code goes with the HTML sent from the server to the browser. The browser is able to execute Javascript.
In order to execute PHP code when you click on a button, there is 2 solutions :
Either this button is a link, or is part of a form. When you click on it, the page is reloaded (you can send it GET or POST parameters if needed), then your PHP code can be executed.
Either you use Javascript and AJAX, and you don't need to reload your current page. Your browser, with Javascript, will send another request to the server "in the back"*, and the server will be able to run some PHP code without you having to reload anything.
*(looking for an English expression that I don't know)
A quick word about jQuery : jQuery still IS Javascript. jQuery is a Javascript library, basically allowing you to have some cleaner, clearer and easier to write code (IMO). But, as halfer mentions in a comment, you don't need it everytime. Other libraries exist, and sometimes, for what you need, "pure" Javascript is enough.
If you place your button into a form with a hidden field like so:
<form>
<button type="submit">Run Queries</button>
<input type="hidden" name="submitted" value="1">
</form>
And use and IF statement around your queries:
if ($_GET['submitted'] === '1') {
// Run queries
}
You get the results it seems you want.
Very simplified: On an initial page load, the queries will be bypassed by the IF statement, which checks the submitted get parameter. When you press the button (submit the form) the submitted parameter will become 1

Using loadUrl to run javascript loads different page [duplicate]

This question already has answers here:
Android WebView always returns null for javascript getElementById on loadUrl
(3 answers)
Closed 5 years ago.
We have an app that uses a form for oauth, just for dev purposes I want to eliminate typing in the user name and password so I added some code like this for when the page finishes loading:
mWebView.loadUrl("javascript:document.getElementById('UserName').value='" + txtUser.getText()+"'");
But for some reason it doesn't fill in the form it makes a new page and just writes out the value of txtUser instead of filling in the input field? Why and how can I fix this?
If you look at a tool look Squirt it does an anonymous function call. So I am not that versed in javascript, but that anonymous call seems to be the key in that the browser sees it as the current page is making the call itself. So try this roughly:
mWebView.loadUrl("javascript:(function(){document.getElementById('UserName').value='" + txtUser.getText()+"';})()");
I faced a similar issue and since I couldn't find a proper solution, I used the following hack -
Fetch the html source text.
Replace </body> tag with </body><script>YOUR JS CODE</script>
webview.loadDataWithBaseURL(url, editedString, "text/html", "UTF-8", null);
Sorry for the hacky solution, but in case you can't find a solution, this could help :)

Keep track of site history? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to keep the browser history in sync when using Ajax?
I think this is a rather easy fix but I can't find an answer anywhere else...so here goes...I made this kind of template for my homepage...I know the code's not insanely elegant but my main problem is that what if I want to send someone to a specific part of my page...rather than just my "home"...take a look http://useless-r-us.t15.org/
I mean how can I reference each of "blag", "projects", and "about me" using some unique url but still have my pretty css3 transitions...I'm thinking something like this...
http://radokirov.com
P.S....I know blag is a typo --> http://xkcd.com/148/
You should use url hashes (the part of the url following the # sign).
Then, in javascript, in the ready (jquery) event handler, based on the url's hash, you should do the appropriate ajax request and populate the page with the appropriate content.
For more details: Modify Address Bar URL in AJAX App to Match Current State

Categories

Resources