How do i become href out of a link - javascript

Following problem:
I would like to not have to copy my navigation bar over every site. So I wrote it in an extra Document and included it with php. The site ur on has a special class to show its active now that where I'm stuck at. I tried following statement just to see if my idea works
<script> if(document.getElementById("test").href == window.location.pathname){window.alert("wow");}
Spoiler alert it does not:(
I know that the pathname is the same as href(tried it) but I can't get the href like that. Does anybody know how to do that? Sorry for my English
Thanks a lot for helping
PS: I'm just getting into js I usually work with other languages but wanted to get into website design.

The problem was simple : document.getElementById("test").href gives out the whole link with www even thoug its not standing in there while window.location.pathname just gives out the path like /index.php. I had to use window.location.href instead.
Thanks for all the answers

Related

Need to take variable input from text box to create a link to use with USPS Tracking System

This is a silly question but I can't seem to find an example that works. Basically I want to add a section on my website for the customer to check their USPS Tracking Number Status; USPS only provides an XML API while I need HTML / JS for this to work.
I need to create a text box that takes a 22 character (or less) alphanumeric variable so that when you click a "Submit" button, it will put the variable into this link.
Sadly, I need the entire script code from the and tags. I have tried every example I found and none of them worked in the sandbox, likely because I am missing some basic tags. I have come to the conclusion that I have no clue what I'm doing so please do not get frustrated by the simplicity of my question.
https://tools.usps.com/go/TrackConfirmAction_input?strOrigTrackNum={xxx} where xxx is must be the variable. Every example of code I have found will append to a link, but not put the variable inside the {}. I have been looking and tried a ton of different examples, but being far from a codemonkey I could really use some help. I apologize in advance because I know this is simple but I don't know what I am doing wrong except for trying to do something I have no business trying to do as I'm not a developer. Thanks in advance!
To provide the answer to my own question here us the correct code,
<script type="text/javascript">
function goToPage() {
var page = document.getElementById('USPS_Tracking_Number').value;
window.location = "https://tools.usps.com/go/TrackConfirmAction_input?>strOrigTrackNum=" + page;
}
</script>

Button as a fake link for a Google Analytics funnel

I'm trying to set up a funnel that has a final step of clicking on a button and downloading a document. Since I can't track the button, I was hoping to set up a fake link that doesn't redirect the user anywhere but allows Google Analytics to see how many people clicked on the button.
What I have is this Javascript piece of code but when it gives me an error:
<a href=”/goal/example” onclick=”javascript:pageTracker._trackPageview (‘example’);”target=”blank”>
Or, if this doesn't work at all, is there any other way to add a button to my funnel?
Thanks!
To piggy-back on #nyuen's answer, (though without your error message it's hard to know what's wrong), it looks like your link could be improved as well. First, since it sounds like you'd rather it not link anywhere, I'd set the href to your code rather than the onclick. And second, if you don't want it leading anywhere a target="blank" is going to be annoying. (Also I assume your <a> is closed eventually). Try:
[...]
There's a couple things wrong with the code looks like. Try this:
onclick="_gaq.push(['_trackPageview', 'example'])"
The javascript part is optional, and also the quotes need to be straight quotes, not the 'smart' quotes.

Broken link - "/a" in JQuery 1.9 Js file

When we had done security audit of our project, we got broken Link "/a" vulnerability.
After searching for link throughout project we found link in JQuery-1.9.js java-script file that we are using in our project.
small part of code in that JQuery-1.9.js -
// Make sure that URLs aren't manipulated
// (IE normalizes it by default)
hrefNormalized: a.getAttribute("href") === "/a",
As per my understanding this code part helps for making it(JQuery) compatible with IE 6/7/8.
hrefNormalized is used to check that anchor tag is giving href value as full URL or exact href , which is issue in IE version.
The better explanation of this part is given in
https://www.inkling.com/read/jquery-cookbook-cody-lindley-1st/chapter-4/recipe-4-1
I want to remove this vulnerability but i don't want to remove or change code in JQuery js file.
So, My question is why did not JQuery designers used "/#" instead of "/a" .What is the problem of using "/#" in that code.
Earlier same question is asked by someone to JQuery Team,but they told that it not the problem from Jquery.
For reference of that ticket
http://bugs.jquery.com/ticket/10149
Help me to solve Or Is there another solution?
Thank you
This is not a vulnerability but a false positive. The security scanner interprets the "/a" string as a link, which it is not.
Even if jQuery creates the link in the DOM, it's not clickable or visible to the user. Your website does not actually have a real link to /a anywhere.
I would ignore the problem without changing anything.
Maybe, if you want this hrefNormalized: a.getAttribute("href") === "/a", to be transformed into this hrefNormalized: a.getAttribute("href") === "/#", but you don't want to touch the jQuery file.
Put that second one in a script in a an order so that the browser reads your script after reading the jQuery file, so it mashes.
Anyway, I never had issues with jQuery before, check your code first.
If you don't want to have your views with scripts, put it in a js file and link this file to your view after the jQuery file.
Hope it helped you, or at least gave you some ideas to solve your problem. Good luck, let us know how it goes! ;)
EDIT:
<script src="~/JQuery/jquery-2.0.3.js"></script>
<script src="~/Scripts/Fix.js"></script>
If you do something like this, the browser reads first the jQuery, then it reads the Fix.js. Inside the Fix.js, you put the function or paramater you want to change from the jQuery.
So the Browser will get the latest one it reads if they are equal.
For example:
function whatever (){ //This in jQuery file
//things #1
}
function whatever (){ //This in Fix file
//Different things #2
}
This way the browser chooses the Fix.js one, because was the last he read.

Command to find a specifc text/link on a webpage, using javascript?

Before I start, I'd like to point out I'm completely new to programming, so I hope my question isn't one that doesn't make sense at all and makes you guys rage. If it is, I'm very sorry, please enlighten me about it.
¨
I'm trying to make a script (in tampermonkey) that will look for a specific text/link on a webpage, and if that text is on the page, it will click on it.
For instance:
Let's imagine we refresh the page and sometimes a link called "Bob" appears. I do not know the target of "Bob", so just asking the script to redirect me to the target link isn't an option. I need something that actually clicks on "Bob". U get me? The problem here is I only know the name of the link, and nothing more. So I'd need a script which, first of all, looks for something called Bob, and secondly, clicks on it.
Thanks for helping,
--Bram
¨
PS: I'm completely new to programming, so if you decide to help me out (which I'd greatly appreciate <3) please explain it like I'm a total idiot/newbie. Thanks!
Just samples are below. Syntax can be wrong but they will give you an idea I think.
//Get all anchors
var anchors = $("a:contains('Bob')")
//Click it
$(anchors[0]).click();
And you can searc on stackoverflow look this links will help you;
How to select all anchor tags with specific text
How can I simulate an anchor click via jquery?

Javascript: Redirecting to another site through a popup

I'm quite bad with Javascript and I can't work out a solution I need help to. I'm having a website and I'm trying to make a redirection to another site, but through a popup.
Example :
<script>alert(This will prompt up the message)</scrip>
<script>window.location="http://This-will-redirect-me-to-another-link.com";</scrip>
Like you can see I could simply use the second javascript to redirect the persons to another page, but due some reasons I can't use it as it will work only for half of the page(the script would be kinda 'sandboxed'), but if I'd make a popup(the first alert script) the second script would get out of the 'sandbox'. Is there anyone who has any ideas how I should implement this or can it be done otherwise with PHP or HTML?
I'm having a MyBB forum and there's a shoutbox for it which I'm using. There's a command which will change the notice of the shoutbox and the command is as such /notice New notice | But I noted that the new notice can be changed with javascript and it'll work such as /notice js code here | Then I thought that what if I would make such a javascript that would redirect people to another webpage. As I'm having such a forum where it's needed to redirect from the main page to another one, I'd like to apply it. Then Staffs could do it in the forum very well, but there's a problem. by adding
/notice window.location="http://This-will-redirect-me-to-another-link.com";
It'll affect only the shoutbox and shoutbox is being redirected to another webpage, but as an alert works for the whole forum I thought maybe I can redirect them to somewhere else with the alert. I want to know is it possible with just one script then Staffs would be able to do it. I know it's a serious security risk & it can be otherwise also, but I'd really like to experiment with it.
I hope someone can help. :)
Try:
window.open('url to other site', 'window name', 'width=900,height=650,scrollbars=yes');
If I understand you (if not please correct me), you can put a tag on popup or another place:
<a href="http://another.site.com/"
onclick="return confirm('Do you want to see another page?');">Redirect</a>

Categories