I am trying to refresh an web page on button click and after page refresh call an function.
But its reverse is not happening i.e. first call the function and after that refresh the page.
This is what I have tried:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>
function restartall(){
$('#text').append('hello');
}
</script>
<a href="javascript:document.location.reload();">
<button onclick="restartall()">Referash Map</button></a>
<div id="text"></div>
please solve this query.
use below code
<body onload="onrefresh();">
<a href="javascript:myfun();">
function myfun(){
window.location.search += '&reload=true';
}
function onrefresh(){
if(gup("reload")=='true'){
// DO whatever want to do.
}
}
function gup( name ){
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null )
return "";
else
return results[1];
}
<a href="javascript:abc();"><script>function restartall() {
$('#text').append('hello');
}
function abc() {
document.location.reload();
restartall();
}</script>
When you reload page you're loosing JavaScript call stack. You would have to append some parameter to URL or use cookie.
try this
<script>
function restartall(){
$('#text').append('hello');
document.location.reload();
}
</script>
<input type="button" onclick="restartall()" value="Referash Map" />
<div id="text"></div>
Related
If I set the URL to exmaple.com?blank=true a blank page should be presented.
Using JavaScript or jQuery i want a short snippet that looks for ?blank=true in the url and if it finds it than the page to turn white or blank.
Try like this
var url_string = "https://example.com?blank=true "; //window.location.href
var url = new URL(url_string);
var blank = url.searchParams.get("blank");
if(blank){
document.getElementsByTagName("body")[0].style.display = "none";
}
<body>
<p>this will be hidden</p>
</body>
wrap your html content in a element like this
<html>
<body>
<div id="wrapper">
<!-- ********your content here******** -->
</div>
</body>
</html>
use jquery code as
$(document).ready(function() {
var sPageURL = window.location.search.substring(1);
var sURLVariables = sPageURL.split('=');
if(sURLVariables[0]=="blank" && sURLVariables[1]=="true")
{$('#wrapper').css('display','none');}
else{
$('#wrapper').css('display','block');
}
});
With jQuery
var blank = /(?<=blank=)[^&?]+/g.exec('https://example.com?blank=true')[0];
if(blank === 'true'){
$('body').hide();
}else{
$('body').show();
}
Hi guys its m first question here y needd to check the actual url and then depending if it is ?lang=es or ?lang=en redirect to https://regular.autobusing.com/paqtur/venta?locale=es or =en clicking on a button with an id=vivesegovia
thats what ive done but its not working
<script type="text/javascript">
function lanzacompra(){
var URLactual = window.location.href;
document.getElementById("viveboton").onclick = function () {
if (URLactual == "http://lasepulvedana.es/testweb/sepulvedana/vive-segovia.php?lang=en")
{
window.location ( "https://regular.autobusing.com/paqtur/venta?empresa=lasepulvedana&locale=en");
}
else
{
window.location ( "https://regular.autobusing.com/paqtur/venta?empresa=lasepulvedana&locale=es");
}
}/*onclic*/
}/*lanzacompra*/
</script>
Thank you very much for your help in advance guys :)
you need to set it by assignment, not as parameter
replace
window.location ( "https://regular.autobusing.com/paqtur/venta?empresa=lasepulvedana&locale=en");
by
window.location.href = "https://regular.autobusing.com/paqtur/venta?empresa=lasepulvedana&locale=en";
I am using javascript to do href. The code as below
<script>
window.onunload = refreshParent;
function refreshParent() {
var SAPno = document.getElementById('HiddenNo');
window.opener.location.href="Inventory.aspx?" + "SAPNo="+ SAPno +;
}
</script>
my hidden field code as below
<asp:HiddenField ID="HiddenNo" runat="server" />
may I know how to use the value in hiddenfield and pass to Inventory.aspx? and anyone know how to retrieve the value?
Thanks!
<script>
window.onunload = refreshParent;
function refreshParent() {
var SAPno = document.getElementById('HiddenNo').value;
window.opener.location.href="Inventory.aspx?" + "SAPNo="+ SAPno
}
</script>
If you want to use JQuery then
$('HiddenNo').val()
The script I wrote has a button that, when clicked, should show year and IP address, but it does automatically. I saw in another 3D that this issue comes when in the "onclick" option you put a function like
<button name="bottone" onclick=myFunction()>AH-AH</button>
but this is not my case.
<button name="bottone" onclick=myFunction>AH-AH</button>
<p id="demo"></p>
<script>
function myFunction(json) {
var d = new Date();
var n = d.getFullYear();
var ip = json.ip;
document.getElementById("demo").innerHTML = n + ' ' + ip;
}
</script>
<script type="application/javascript" src="http://jsonip.appspot.com/?callback=myFunction"></script>
Seems like the second script "overrides" the option onClick.
The script calls your function immediately:
myFunction({"ip": "88.9.35.40", "address":"88.9.35.40"});
Instead, you could store the data in a variable, and use that variable in the event handler:
<button name="bottone">Display year and IP</button>
<p id="demo"></p>
<script>
(function() {
var ipData;
window.getIpData = function(data) { // We must use awful global for JSONP
delete window.getIpData; // Get rid of the awful global
ipData = data;
};
document.getElementsByName('bottone')[0].onclick = function() {
if(ipData) {
var year = new Date().getFullYear(),
ip = ipData.ip;
document.getElementById("demo").innerHTML = year + ' ' + ip;
}
}
})();
</script>
<script type="application/javascript" src="http://jsonip.appspot.com/?callback=getIpData"></script>
Change that onClick to onclick="myFunction()", that's how it should be (always use the quotes, it's a function call so include the parenthesis).
That second script invokes directly the function you are passing as callback parameter ?callback=myFunction, so it will call autonomously your myFunction().
Try opening that script url directly in your browser to see what the script will execute:
myFunction({"ip": "11.11.11.81", "address":"11.11.11.81"});
Use this piece of code. The script u included, was calling a callback fucntion, and inside that it was printing Date and Ip onload of page.
So I have kept that showing ip and date code in separate function.
The variable which are used for display of date and Ip> I made them global, so those variable can be used outside callback method.
<button name="bottone" onclick="showIpNDate()">AH-AH</button>
<p id="demo"></p>
<script>
var ip, n;
function myFunction(json) {
var d = new Date();
n = d.getFullYear();
ip = json.ip;
}
function showIpNDate() {
document.getElementById("demo").innerHTML = n + ' ' + ip;
}
</script>
<script type="application/javascript" src="http://jsonip.appspot.com/?callback=myFunction"></script>
I am new to web development.
Today I learn about classes(function) in javascript. I got an error how to call dynamically added method.
My Code :
<head runat="server">
<title></title>
<script type="text/javascript" language="javascript">
function MyMethod(name, fn) {
var str = "MyFunction1.prototype." + name + "= fn;";
eval(str);
}
function MyFunction1() {
MyMethod("start", function () { return "hi"; });
var abc = this.start(); //gives error
}
</script>
</head>
<body>
<form id="form1" runat="server" >
<div>
<input type="button" value="Click" onclick="MyFunction1()"/>
</div>
</form>
Here when I click the input button then not able to call the dynamically added function
How can i call the start() function that i added here.
Please Help me.
Thanks in Advance.
this in MyFunction1 referes to the global object in that case(for browsers it is window) , because you call MyFunction1 as function and you don't create an object by using new MyFunction1().
Another thing to be noted. You should not use eval when it is possible to do it without eval.
You can do the same thing using:
function MyMethod(name, fn) {
MyFunction1.prototype[name] = fn;
}
Using eval prevents you from using optimization tools or tools to validate your code. At least most of these tools don't take eval into account or even give a warning about that you are using it.
Try adding "new" before your onclick call to MyFunction1, creating an instance of it.
It reseolved I did
Hi , It resolved .Thanks for the gret help i did :
function fnOnload() {
MyMethod("start", function () { return "hi"; });
}
function MyMethod(name, fn) {
var str = "MyFunction1.prototype." + name + "= fn;";
eval(str);
}
function MyFunction1() {
}
function MyFunction2()
{
var aa = new MyFunction1();
var answee = aa.start();
}
and in click of button i callled function MyFunction2()
without changing your code you can do as follow , but I say it would be helpful if you read about invocations types and about this variable.
function MyMethod(name, fn) {
MyFunction1.prototype[name]= fn;
return MyFunction1.prototype;
}
function MyFunction1() {
var myPrototype= MyMethod("start", function () { return "hi"; });
var returnValue = myPrototype.start();
console.log(returnValue);
}