How to reload an html link/reference - javascript

I have the following link in my HTML file
<p class = "card-text"> This is a title </p>
Text
how can I have that after I click the button and go to google, every certain block of time say 30 seconds, google is refreshed/reloaded

You can't.
There is no general way for a page to affect the next page loaded into a browser
There is no Google-specific method to cause the page to reload on a timer
It is possible to do something along these things with a page loaded into an <iframe>, but Google set the X-Frame-Options to forbid third-parties loading their homepage into a frame.

Just write an function with window.setInterval() and window.open() like this:
window.setInterval(function(){
window.open(URL, yourWindowName);
}, 30000); // Wait 30s
Don't forget to stop it with clearInterval()

Related

What is the proper way to wait for a new URL to load in nightwatch?

A page I am testing has a button that takes you to a different page on the same site. After clicking on that button, I want to wait for that page to load before continuing. Normally, I would just wait for some element on that page to load, but since I recently updated nightwatch/selenium, that waitForElementPresent() test has stopped working. In the process of debugging the problem, I thought it made sense to wait for the new URL to load, but I don't see a nightwatch way to do that. I can hard code a wait with a pause() followed by an assert.urlContains(), but there's got to be a better way. Any suggestions?
What used to work:
this.waitForElementVisible(runCSS,3000)
.click(runCSS)
.waitForElementPresent(newPageElementCSS,5000)
but now it times out on the second wait, even though I can clearly see the new page on the browser display (Firefox 45.0.1 on Windows 8.1).
Wait for something (a selector) that is unique to the page that will be loaded after the click. It can be anything as long as it doesn't exist on the current page.
For example,
This would wait for <div name="Thingy"> anywhere on the page:
client.waitForElementVisible('div[name="Thingy"]',3000)
I guess, in Nightwatch.js, wait for the page to load can be achieved using 'body' element of the DOM. Suppose, if we want to wait the page for 1 second. we will achieve it using the command .waitForElementVisible('body', 1000). The second parameter is measured in ms.

Inline javascript to refresh a webpage automaticaliy at given interval and check for a change in a div element

I am trying to make an inline script (a script to put as a bookmark in the browser) that needs to refresh a webpage automatically at an interval (15 or 20 seconds) and check for a change in an element. Than if that elements meets certain criteria, to click on a button.
Imagine an ebay listing, the script checks the html element with the price and compares it to a value stored in the session storage. If the price is below a given price it clicks on a button to buy the item.
I can't put any code in the website, all I can do is execute the inline script from the bookmark.
Basically I want to click on the bookmark and from there on the page needs to start refreshing every 15 seconds until the tab is closed.
I found this code:
javascript:
var current = location.href,
timer = setTimeout('reload()', 20000);
//if I put the logic here it only executes once and then does not check for a change in the div element
function reload(){
//if I put the logic here it does not have access to the DOM and can't check for the changes.
setTimeout('reload()',20000);
fr4me='<frameset cols=\'*\'>\n<frame src=\''+current+'\'/>';
fr4me+='</frameset>';
with(document){
write(fr4me);
void(close())
};
}
But as mentioned in the comments - the document.getElementById either works only once if placed at the top, or it doesn't work at all if inside the reset function.
So any suggestions? Thanks for looking
It will be difficult as a bookmarklet because the page (and any changes) will disappear in each reload. There are ways to work around it, but it won't be easy.
If you are using Chrome, Safari, Opera or Firefox, it would be far easier to create a UserScript that runs outside the page to get the affect you are after.
setTimeout will reload your page once when the document is ready.
What about using setInterval?
Example:
setInterval(reload, 20000);
Each 20s, your page will be reloaded.

Better way to refresh page and not scroll to top

So I have a refresh code that allows me to refresh the page every few seconds, however there are a lot of images that do this flash thing on each couple second reload wondering if there is a better way? This is what I have any help much appreciated! The images only flash if the page is not scrolled to the top??? I don't quite understand, but I have my theory.
If you would like to refresh a specific portion of the page every few seconds, you could simply separate the part of the page which you wish to reload into a div, then refresh it like so:
setInterval(function(){
$("#yourdiv").load("yourpage.php #yourdiv");
}, timeout);
Where #yourdiv is the name of the div of the site which you want to reload, yourpage.php is the page which you wish to reload it from, and timeout is the amount of time you want it to reload in. For example, if I wanted to refresh #content every 45 seconds from index.php, I could do this:
setInterval(function(){
$("#content").load("index.php #content");
}, 45000);
This solution uses jQuery, so you'd need to include the jQuery libraries with
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
before that function.

Run jQuery, reload, rinse and repeat...how?

I have the following code;
<script>
$(document).ready(function() {
$('a[href*="profile"]:contains("PETER PAN")').closest('tr').find('.fightActionInnerInner').click();
});
How would I run this, then refresh the page (say every 2-3 seconds) and rerun the script. I'm using Greasemonkey, if that helps. Thanks.
You can use the setTimeOut function with a window.location reload like this:
$(document).ready(function(){
setTimeout(function(){
window.location.reload();
}, 2000);
});
Here is the fiddle of a working example:
jsFiddle
In JavaScript you can reload the page with window.location.reload(), history.go(0) or even window.location.href=window.location.href
The code in document ready function will automatically run again on page reload.
If you want to delay something, you can do this with setTimeout:
setTimeout(function (){
//do something
}, yourMillisecondsToWaitUntilStart);
For your code it would be:
$(document).ready(function(){
$('a[href*="profile"]:contains("PETER PAN")').closest('tr').find('.fightActionInnerInner').click();
setTimeout(function(){
window.location.reload();
}, msToWait
});
Replace msToWait with the number of milliseconds you want to delay the page reload.
Read about the Meta refresh.
You just place this inside the head tag of your page
<meta http-equiv="refresh" content="3">
However, I suggest you read the whole page, specifically these parts (even if you end up using the javascript way of redirecting which other users have suggested since this text shows some general drawbacks of refreshing every few seconds, no matter what way you do it):
Use of meta refresh is discouraged by the World Wide Web Consortium (W3C), since unexpected refresh can disorient users.
Drawbacks
Meta refresh tags have some drawbacks:
If a page redirects too quickly (less than 2-3 seconds), using the "Back" button on the < next page may cause some browsers to move back to the redirecting page, whereupon the > redirect will occur again. This is bad for usability, as this may cause a reader to be "stuck" on the last website.
A reader may or may not want to be redirected to a different page, which can lead to user dissatisfaction or raise concerns about security.
Alternatives
For refresh
An alternative method is to provide an interaction device, such as a button, to let the user choose when to refresh the content. Another option is using a technique such as Ajax to update (parts of) the Web site without the need for a complete page refresh, but this would also require that the user enable JavaScript in their browser.
If you don't really need a page refresh, I suggest you use setTimeout javascript function, as already mentioned in another answer (except use it to trigger the click, not reload the page) since refreshing the page is a big thing to do for something small (if the click does something small, which I'm guessing it does).

How to trigger click-to-call with javascript (iphone)

I have a link in a mobile webpage that needs to track an advertiser clickTag and then activate click-to-call.
I've got the tracking working but I don't know how to trigger the tel:1800123456; with javascript. Any ideas? This is not a web app; it's a standard html page. I can use jQuery.
Update
Just calling window.open("tel:num"); after adding a tracking iframe on click was not reliable enough because sometimes the call dialog box would open before the iframe had finished loading.
window.open("tel:num"); also opens a new window then opens the call dialog box, which isn't a great user experience on iphone 3gs/4.
Do you have any control over the tracking iframe? If so, you could call a function which makes the window.location call once it's loaded. Something like
$(document).ready(function() { window.iframe_loaded(); });
in the iframe code (if it has jQuery), and a function in your main script called iframe_loaded which does the window.location call.
If you can't set the code within the iframe but can edit the iframe container code, then you could do this...
<iframe id="whatever" onload="iframe_loaded();" width="400" height="200"></iframe>
...so the onload calls iframe_loaded() which does window.location...
If you don't have control over the iframe or its content, then easy kludge would be to just wrap the window.location call in a timeout, i.e.
setTimeout('window.location="tel:18001234567";', 500);
The 500 at the end will delay it by half a second. (Increase it if your iframe is slow to load.) It's not as elegant, but might work fine and users probably won't notice a small delay!
Have you tried window.open(url); where the url is "tel:18001234567" ?
Seems like that should work, right?

Categories