I want to save some JavaScript code as a bookmark in chrome so it automatically opens my university login site and clicks on on the login button. I am completely inexperienced in JavaScript, so I have no clue how to do this. I snipped together the following code, which opens the correct website, but then does not click on anything. The first URL automatically puts me to the login site (third URL in the code) in case I have not logged in yet in this window.
(function() {
window.location.replace("https://lms.uzh.ch/auth/MyCoursesSite/0");
window.onload = function(){
if (current_url.startswith('https://lms.uzh.ch/auth/MyCoursesSite/0')) {
return;
}
if (current_url.startsWith('https://lms.uzh.ch/dmz/')) {
document.getElementById("wayf_submit_button").click();
}
};
})();
I'm sorry if this is too obvious a question and annoys any experts but as I said I am a complete beginner. I would of course add the "javascript:" at the beginning for chrome to understand the bookmark.
When you use window.location.replace, you change the address and you code can't work anymore.
I can suggest using some browser extension, your "click function" should work then.
I guess you could also try to make some simple html page with iframe, you call your "click function" at this page, but you target it to the iframe. After that you can change browser's location to the university's webpage as you should already be logged in.
<html>
<head>
</head>
<body>
<iframe src="your_university_address.com" id="some_id" onload="click_button()"></iframe>
<script>
function click_button()
{
my_iframe=document.getElementById('some_id');
my_iframe.contentDocument.getElementById('your_button_id').click();
}
</script>
</body>
</html>
Very simple but it should do that job.
(You can achieve similar result by using window.open() I guess)
Related
So I have a google site that I use for html hosting and I want to make a button that replaces the current window with a search window. So I do the normal routine:
<button onclick="doThis();">🔎</button>
<script>
function doThis(){
window.location="https://sites.google.com/example/search";
}
</script>
It replaces the window but just says "sites.google.com refused to connect".
Anyone know what's going on?
Try to use
window.location.assign()
<script>
function doThis(){
window.location.assign("https://sites.google.com/example/search");
}
</script>
And "https://sites.google.com/example/search" does not exist.
If you want to go to the search page, use: "https://www.google.com/"
If you want to use a goodle search engine for your website, you have to do something like this:
Passing html form values to google search query
If we have some pictures shown on the page as thumbnails, and when the user click on the picture , the page show the full picture using any JavaScript or JQuery methods.
how we can change the browser url address to point to picture instead of the main page website while keeping the user on the current page.
for example:
if we visit any instagram account for example
http://instagram.com/fofo
and when we click on any photo the script will change the address url in the browser to
http://instagram.com/p/ReTycBy2Bj/
for example
how we can do something that?
Thanks for help
You can achieve that by manipulating the browser history using the history.pushState() method on supported browsers. See https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Manipulating_the_browser_history#Adding_and_modifying_history_entries for details.
Here is an example of a script that will solve my question
After review +lshearer answer and start searching about "pushState" and while im searching I found this url http://html5.gingerhost.com/seattle which helps me to understand how to change the URL of browser address bar without loading it's content.
<html>
<head>
<title>some title</title>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
</head>
<script>
$( document ).ready(function() {
// Handler for .ready() called.
var original_url = window.location.pathname;
$("#show").click(function() {
href = "picurlhere.jpg";
// HISTORY.PUSHSTATE
history.pushState('sss', 'New URL: '+href, href);
});
$("#close").click(function() {
// HISTORY.PUSHSTATE
history.pushState('sss', 'New URL: '+original_url, original_url);
});
});
</script>
<body>
<button id="show">showpic</button>
<button id="close">closepic</button>
<pre></pre>
</body>
</html>
I tested it on FireFox 25+, Chrome 31+
And it works.
When I click on 'showpic' button the script will change the url to 'picurlhere.jpg' and when I click back to 'closepic' button it will return me the original page URL.
This will help us while showing images using JQuery or JavaScript methods so we can change the browser url because if the user copy the link and send it to any one it will point him to the picture url not to the site page. and that is exactly used on instgram.
but in real show you have to make this script more smarter.
Thanks for Help.
No need to use any script, just plain HTML. Wrap your image like this:
<img src="your-image.jpg" alt="">
I need JavaScript code or HTML to make two websites open in two new browser tabs when clicking on one link. I do not want them to open in new windows, or on the current page that the link is on.
It probably won't work because the browser might consider it a popup and block it.
If the user allows popups you can do:
window.open(url, '_blank');
Like:
<a id="mydoublelink" href="http://site1.com" target="_blank">foo</a>
document.getElementById("mydoublelink").onclick=function(){
window.open('http://site2.com', '_blank');
}
If you call window.open in the onclick event you should be fine. Built-in popup blockers allow those. The kind of popups that get blocked come from other events or from scheduled events like a setTimeout.
document.getElement("my_link").onclick = function () {
window.open(/*..*/); // works
}
document.getElement("my_link").onclick = function () {
setTimeout(function () {
window.open(/*..*/); // will probably get blocked
});
}
This means, for instance, that if you open a popup after an AJAX call it will very likely get blocked. A workaround in this case is to open the popup right away and fill in the content later. This is outside the scope of this question but I feel like this is information that everyone should know.
Something like this?
<!DOCTYPE html>
<html>
<head>
<script>
function open_win()
{
window.open("URL");
open_win_two();
}
function open_win_two()
{
window.open("URL");
}
</script>
</head>
<body>
<a onclick="open_win()">luyfl</a>
</body>
</html>
Yu can try the new target _newtab:
blabla
It works in Firefox, don't know if it's supported in other browsers.
I have a page with an iframe in it. Within the iframe, the default page runs some javascript to open a child window, login to an external service, then display some content from that external service back in the iframe.
My code works fine in Chrome and IE but in Safari and Firefox it does not. In those browsers the parent window seems to ignore that fact that the user is now logged in to the external service and displays their "Not logged in" message instead of the info that is supposed to display.
Here's the code for what I'm trying to do.
HTML (main page):
<iframe id="brief" name="brief" src="contents/somepage.php?var=WXYZ" width="962" height="600">
<p>Your browser does not support iframes.</p>
</iframe>
somepage.php
<html>
<head>
<script type="text/javascript">
window.onload = function()
{
var code = 'WXYZ';
var login = http://www.externalsite.com/brief/html.asp?/cgi-bin/service?msg=0048&usr=username&pwd=pass';
//OPEN CHILD WINDOW AND LOGIN USING VARIABLES ABOVE, THEN CLOSE WINDOW
childWindow=window.open(login,'','width=30,height=30,location=no');
var cmd = 'childWindow.close()';
setTimeout(cmd,2000);
//REDIRECT THIS IFRAME TO BRIEFING INFORMATION
var uri = 'http://www.externalsite.com/brief/html.asp?/cgi-bin/service?msg=0092&AID=NOSEND&MET=1&NTM=1&LOC='+code;
self.location.href=uri;
}
</script>
</head>
<body>
</body>
</html>
I have tried adjusting various setTimeout functions to try to delay certain aspects of the script to wait for something else to happen but it doesn't seem to help.
Is there some cross-browser problem with this code that I am missing?
Thanks
setTimeout(cmd,2000); is not going to block script execution for two seconds; rather, it sets up an event that will fire in approximately 2 seconds. Immediately after your call to setTimeout, the remaining parts of the script will execute:
// This happens right away
uri = 'http://www.externalsite.com/brief/html.asp?/cgi-bin/service?msg=0092&AID=NOSEND&MET=1&NTM=1&LOC='+code;
self.location.href = uri;
The fact that it works in any browser is just lucky timing. If you want the iframe to refresh after the popup closes, add that code to your callback (you don't need to and shouldn't use a string for your timer handler, by the way):
setTimeout(function() {
childWindow.close();
var uri = 'http://www.externalsite.com/brief/html.asp?/cgi-bin/service?msg=0092&AID=NOSEND&MET=1&NTM=1&LOC='+code;
self.location.href = uri;
}, 2000);
Even this solution won't work if the popup's content doesn't load within two seconds. The best solution is to have the popup close itself after loading. Then, you could detect when it closes and know that you're ready to reload your iframe. But if this is a third-party site that you don't have control over, you're probably stuck using a less-than-ideal solution like this.
Morning All,
I was hoping someone could help or provide some sample code for me in refe to the following. I not too sure if i can simply set the asp.net (Visual Studio 2010) properties or if i need some java script to complete my task.
I have a web page with a link on it that when users click it opens to a new web page via the _Blank command.
What i would like to do is have this page open on a really small scale (640px X 480px) and layerd on top of the main webpage. This small page essentially just holds a gridview with items listed for documentation.
I have looked around the internet as i think this would be best done by using JavaScript in the smaller web page but i cant get this to work successfully.
I have found a sample and need to try and tweak this but have been unsuccessfull.
Here is the sample code i have....
<script type="text/javascript" language="javascript">
window.onload = function () {
var w = window.open("about:blank", "main", "width=640,height=480", true)
window.opener = "main";
window.open("", "_parent", "");
w.opener.close();
}
</script>
My smaller web page is named uploadview.aspx
Any help is advenace is much appriechiated.
Regards
Betty
You got to specify window URL in first param of window.open, and must have button to invoke that JavaScript, it's not clear why do you do that on page load event. Anyway, here is the example to open window on some link click
Open Upload View
<script type="text/javascript">
function OpenUploadView() {
window.open("uploadview.aspx?param=1", "_blank", "width=640,height=480", true);
}
</script>
You can also pass parameters to that aspx page like shown above.
You can use window.opendialog()
For more visit https://developer.mozilla.org/en/DOM/window.openDialog