Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The last example on "How to Create a Basic Plugin" returns a different result than the previous example. #739

Open
Jeff-Schafer opened this issue Dec 16, 2016 · 5 comments

Comments

@Jeff-Schafer
Copy link

Jeff-Schafer commented Dec 16, 2016

The last example on https://learn.jquery.com/plugins/basic-plugin-creation/ says:
"Our plugin can be optimized though:"
and shows the "optimized" code.

But the example before it, when run, just appends the text in the href for that tag, & this "optimized" code appends the full path to where the .html file is to what is in the href for that tag.

The first example under the "Putting It Together" correctly appends "page.html" to the text between the tags.
---> Foo (page.html)

Using a local .html file (like on the Desktop), the "optimized" returns:
---> Foo file:///C:/users/..../Desktop/page.html (without the parentheses, unlike in the first example).

Below the last example, it says:
"Notice also that we're not using the .attr() method to retrieve the href attribute, because the native DOM API gives us easy access with the aptly named href property."

To make it have the same results as the first example in this section "this.href" needs to be replaced with "$(this).attr("href")".

This will contradict the text mentioned above ("not using the .attr() method"), and needs to be updated.

@dmethvin
Copy link
Member

To make it have the same results as the first example in this section "this.href" needs to be replaced with "$(this).attr("href")".

Not sure I understand. Are you saying you get different results with this.href rather than $(this).attr("href")? Can you post an example?

@Jeff-Schafer
Copy link
Author

Yes. See the attached Screenshot & .html doc code of all 3 examples.

How to Create a Basic Plugin.zip

how to create a basic plugin

@karan-96
Copy link

As far as i know 'this.href' returns the absolute url as it directly uses the javascript implementation of the browser because of which it combines the interpreted href attribute with the current host URL.
Whereas '$(this).attr("href")' wraps the element in a jquery object and returns simply the exact string which is in the HTML.

So,I think we can do one of the following to fix this issue

1.We can either use '$(this).attr("href")' in the optimized code and remove the sentence
"Notice also that we're not using the .attr() method to retrieve the href attribute, because the native DOM API gives us easy access with the aptly named href property."

2.We can add to the information provided below the code saying that "this.href will return absolute URL.In case you need relative URL as in above code you can use '$(this).attr("href")'."

I would like to take up this issue.So,please guide me which approach should I follow.

@Jeff-Schafer
Copy link
Author

Jeff-Schafer commented Jan 16, 2017 via email

@karan-96
Copy link

@dmethvin which approach should i start working with '1' or '2' or what Jeff-schafer has suggested which is a combination of both?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

4 participants