jQuery button click when page load, keeps looping? - javascript

I Have a chrome extension, it click a button when the page is loaded.
$(document).ready(function(){
document.querySelectorAll("input[type='submit']")[0].click();
});
This works, the only problem is that the button is also on the next page. So it keeps pressing the button and the page keeps reloading, so it's in a loop.
How do I fix this?

One solution would be to refine your selector. Find an unique parent element for the page you're targeting and create a more specific selector.
Another solution would be to check the current page url or the referrer and see if it matches a certain pattern.

Related

Have Window Keep Focus After One Click Fires Two Page Redirects

First of all, happy holidays to everyone.
I have an anchor element that fires two page redirects - one through the href attribute, one through jQuery.
<a id="myClick" target="_blank" href="http://www.google.ca">Google</a>
$('#myClick').on('click', function () {
window.location = 'http://www.stackoverflow.com';
});
This works fine, but the new tab gains focus. I'd like the original tab to keep the focus for the user. I did give window.focus() a go but, as assumed, it didn't work.
I also did try working with the fiddle from this answer but it only fired the new tab and the original page (jsfiddle) remained the same URL. My edited Fiddle from linked answer
How could I go about having the original tab keep the focus? Is it out of my hands?
You can't open tabs in the background using javascript because this is set in the user's preferences in about:config, which you have no control over. The setting is:
browser.tabs.loadDivertedInBackground=true

Stay on same tab after postback

I am using Samson O's very nice Easy Responsive Tabs to Accordion plugin on an ASP.NET page. On Tab 2 I have a Search button that when clicked executes some code in a code-behind and returns the search results to the page. Everything works except that the page goes back to the first tab when it's finished running the code in the code-behind. I want it to stay on Tab 2. How can I achieve this?
I have tried calling a JavaScript function to simulate the click of Tab 2 ($('#mycharities').click();) from the OnClientClick event of the Search button and also as the last thing on the Search button click event in the code-behind. Both options run the JavaScript but the page still goes back to the first tab once finished.
Answer to your question lies in the script for this plugin :
//Active correct tab
$($respTabs.find('.resp-tab-item.' + options.tabidentify)[tabNum]).addClass('resp-tab- active').css({
'background-color': options.activetab_bg,
'border-color': options.active_border_color
});
If you notice it takes [tabNum] to set the active tab. So you need to figure out the last selected tab num and in your custom javascript function that you need to set the appropiate css class to that tab. Then u will have it selected after the postback.
Best yet try to submit using AJax. It easy and you can avoid hassles.

Cannot find element after page refreshed - Selenium

I wanted to run a selenium test that checks whether it navigates to a new page when the button in the old page is clicked. Then I wanted to test whether it navigates back to the old page when the button in the new page is clicked.
The test works correctly when i click on the button on the first page and check whether the button element on the new page appears. But when i use selenium to click on the button element on the second page and check if the button element on the first page has appeared, it fails to find the button element on the first page, although i used the same button element to go the second page the first time and i can see that the first page is loaded. i tried usign the following wait statements:
waitForPageToLoad()
waitForTextPresent("", "text on first page")
waitForElementPresent("" , //path to first button element)
WebElement element = (new WebDriverWait(driver, 10)).until(ExpectedConditions.elementToBeClickable(By.xpath("path to first button elment")));
None of the above wait statements work although the same element was found when the page was first initialized.
Realized that the second time the first page is refreshed i had to first do driver.switchTo().frame(driver.findElement(By.xpath("path to iframe")) as the application was inside an iframe. So it could not find the elements the second time, unless i first switch to the iframe inside.

SharePoint <button> elements perform an unwanted reload of the page

I have an aspx page on my SharePoint site, which I have included tags. For some reason, every button on the page will reload the page when clicked. Even the buttons with no attributes (id, class, etc) or functions will reload the page when clicked. How can I fix this issue? I can't even see what's going on in the debugger because I'm not calling any reload functions, so I have no idea where to place a breakpoint.
Thank you in advance for your help, I really appreciate it.
The problem here is with the <button> tag. Its default behavior is to act as a submit button unless otherwise declared and will reload.
To keep your <button> tag, add type='button' to the button element. I think that prevents the reload.
Or you could go with the ole <input> tag with a type='button'. That keeps the reload from happening as well.
Or some other html element with an onclick event will work too.
First search for a function called doPostback and set a breakpoint on the entry point and click a button. If you hit this breakpoint it could mean that auto post back is turned on for the control generating the button. However if you trigger that breakpoint you should be able to look at the stack trace to figure out how you got there.
If that doesn't work, use the F12 tools in the browser, start with the HTML section and search (Ctrl-F) for the word "click". Then go to the script tab and do the same for each JavaScript file. If all of the buttons exhibit the behavior there is most likely a click event registered. Possibly with jQuery that looks like this $('button') so that it matches all buttons on the page and registers a click handler.
If that doesn't find it, and you have access download one of the master pages from http://startermasterpages.codeplex.com/ and temporarily replace your master page with one of these. Take a screenshot of the scripts that are loading on your page first. Then add them to the starter master page one at a time until the unwanted behavior returns. Then set a breakpoint on every function entry point in that script and click a button and see where you land.

How to avoid page scroll-up when a link gets clicked

I have a div which is half way down the page. After I click a button, I load an image in that div. What happens is, that the page scrolls all the way up. How to avoid this ?
you have to edit your click event. the simplest thing would be to return false. you could also preventDefault f.e. if you are using jquery.

Categories