This is gonna sound like a dumb question or maybe it is a dumb question and I apologise in advance, but I am having a problem trying to get my window.location.href to show an alert on webpages like google and youtube.
It shows alerts on the live server fine, however its just that I can't tell if its working on webpages like google and youtube as I am not seeing any alerts. When I do run it, I do see it re opening the tab I just minimised, so it maybe working, but is there any other form of validation too see if it is working.
I'm still pretty new to JavaScript but this is the code I'm using, any advice would be much appreciated. Thank you again
<p id="URLc"></p>
<script>
document.getElementById("URLc").innerHTML =location.href
alert('This website is'+ window.location.href);
</script>
I see that your question isn't clear enough.
However, if you want to navigate to another web page from yours and display an alert there. You can not display alerts in web-pages which you don't owe because the page you open will have a separate script and your JavaScript code won't affect it.
You can display the alert before changing the location.href.
You can use postMessage() as illustrated here.
You can display a pop-up window with the message you want to display, by making another HTML file, adding the message you want to it, and doing like this:
window.open( {{THE HTML FILE URL}} , 'popup', 'width=600,height=600');
window.location.href = {{THE URL YOU WANNA NAVIGATE TO}};
Not That you're able to pass the URL you want to navigate to to your HTML file using Query Strings
If your question is about displaying your location.href on your website, it should just work fine. I opened https://www.youtube.com/?feature=ytca, opened the browser console and ran alert(window.location.href); and the alert got displayed.
If you don't mean this, please make your question more clear.
Related
I'm wanting to be able to theoretically pass a URL to a window.print() (yes I know you cannot pass a url to this function) via a button. Then the print window would pop up with a page/article that you are not currently on in the browser.
For example: Let's say I am on Facebook.com and I have a share button for an article that pops up on Facebook.com. Lets say that when you click share, there is a print button that when clicked it will print the article (let's say this article is google.com for example). When I am on Facebook.com I want to be able to print Google.com using that share button WITHOUT having to even go to the website/page Google.com.
Is this something that is possible with javasript/Jquery. I have been doing a lot of research into it but not finding anything that would fit this criteria.
Thank you for your help!
I have tried setting up a jquery function that takes in a doc url and print the window but it continues to print the current page that I am on.
I have tried doing a lot of research on the idea but am continuing to see problems where people are only trying to print the page that they are currently on.
It is always possible if you work on it, with a little bit of creativity.
I imagine you can fetch the content of the page through iFrame element, keep the element hidden and then print the content of it.
Found that Link that might help you
Actually this is what I'm trying to do. I have a form and when a user fills that form it goes to a thank you page where thank you message is displayed. Now what I'm trying to do is as soon as the thank you page opens a pdf file should start downloading without the user clicking anywhere on the page. I tried to find a solution by searching but almost all of them have given a solution where one link is provided and user have to click that link to download her pdf. But that is not what I want. And I want to achieve that using Javascript or jQuery only, no server side language.
A clarification:- PDF download on clicking a link is already working. What I want is PDF to download as soon as thank you page opens. I know logically this should not happen because by doing this anyone can set any number of files to be downloaded as soon as their page opens and fill your local drive. But my client wants only this thing to happen.
Calling click() for the user on an other-wise working link should do the trick:
<html>
download pdf
<script>
var link = document.getElementById("link");
link.click()
</script>
</html>
You can do it, in the HEAD tag:
<meta http-equiv="refresh" content="0; url=yourfile.pdf">
This will work also with browsers without JavaScript, and as soon as the the head tags loaded.
a working example:
See an example on github
You can use ajax to hit the url when the page loads.
EDIT: You might want to check this out https://stackoverflow.com/a/29266135/4549494
I am a newbie to extensions. i want to create a extension, when clicked on the icon it should open up the www.gmail.com, input my user name and password and login automatically,click on the first mail. Your help is highly appreciated. I tried many things like chrome inject api but did't work out.
Like said in comments, go have a look to documentation sample codes, it's a good start.
For your extension you should do something like this :
Open a tab with the gmail.com url.
in the callback, inject a content script in the newly created tab
In the content script put the login and the password in page input and then simulate a submit button click.
Try to do this for a first try. When you get it work, You can try to open the latest email received. Look for tabs and runtime message api to send data or event between injected scripts and background script.
I am trying to load a webpage then insert my own Javascript into it.
I have the current code here:
window.location.assign('http://http://79.170.44.75/hostdoctordemo.co.uk/downloads/vpn/index.php');
document.getElementById('address_box').value = prompt("Site Address: ");
document.getElementById('go').click();
and what I am trying to do is:
Load the webpage
Set the address box to a value
Simulate a mouse click on the search button
So it loads the webpage, then searches a value it sets itself.
The problem with my current JavaScript is that as soon as the webpage has loaded the JavaScript stops working (as I expected). I have tried using the iframe tag to load the webpage 'within the webpage' but that did not work when obtaining the id and people said iframe would also not work because of the resolution difference.
**The Question: ** How do I load a webpage and run my own JavaScript code on it? Thank you!
Matthew
You're propably looking for something like Greasemonkey.
I really can't see an easy way to do what you want.
When the browser receives a web page from a server the javascript is interpreted, and only after that, the page is presented on the screen.
So you would have to have a web page with a button or other mechanism to make a request to a web server, receive the request, save its contents locally, add your javascript code and only then "give it to the browser".
I am trying to get this functionality going but am a bit uncertain and don't know how to approach it. I have a master page with a div called "masterDiv". 'masterDiv' makes a load() call and loads content of an external html page called "details.html" from it content div. This is how I am doing it:
$('#masterDiv').load('details.html #content');
content loads up as expected and the url address pops in as "http://www.xyz.com#details"
This is all good and working, but then I thought of those users who may not have JavaScript endabled. I figure I would just direct those users to 'details.html' page directly instead of having the "Master Page" load the content from "details.html" page. So now here is the issue, lets say if I send a user this link:
http://wwww.xyz.com#details
And if that user's browser doesn't have Javascript enabled then obviously JQuery cannot be invoked and therefore load() call will not be made and so on. how can I direct the user to "details.html" page directly, please?
Any insight would be wonderful
Thank you.
Your link probably looks like this :
<a id="myLink" href="#details">Link that the user clicks</a>
When the user clicks the link, jQuery load is called. Is that correct?
If so, you could instead have your link like this :
<a id="myLink" href="http://wwww.xyz.com/details.html">Link that the user clicks</a>
That way, when the page loads, the link will work for everyone (even those with javascript disabled). Then, when the page first loads :
$(document).ready(function() {
$('#myLink').attr('href', '#details');
});
will set the link to the way it was before. That way, only users with Javascript enabled will use the load version. The other ones will simply be redirected to details.html
If there is something I haven't understood correctly in the question let me know.
how can I direct the user to "details.html" page directly, please?
By making the link's href attribute "details.html". The way every link works by default.
Details
Every link on your site should be built this way. That is how the Internet is designed to work, and how it works best. You should only add functionality with JavaScript if you actually need to, you shouldn't be depending on it for something as fundamental as linking between pages.