0

I have a parseuri jQuery plugin method that affects the .val() of an input otherwise the element's .text(). Although this question is general, this specific method parses URLs for example:

// assume text box contains URL like [ http://example.com:80 ]
$('input:text').parseuri().authority; // get the authority of the domain

// or if in a div
$('div#url').parseuri().port; // get the port of the url

How can I call this jQuery method on a standard string without using a selector?

Context

The plugin is my own, based on the jQuery Maintaining Chainability code sample here and it wraps this parseUri 1.2 JavaScript method.

I pasted my initial attempt here: http://jsfiddle.net/wyPKH/

I was hoping to have a plugin version of the parseUri JavaScript method that can operate on both elements and plain strings in a consistent manner.

4
  • 1
    That depends how the plugin is set up. Do you have a link to it? Commented Nov 20, 2011 at 0:08
  • 1
    Can you post a link to the plugin? Commented Nov 20, 2011 at 0:09
  • Added Context to the question. Commented Nov 20, 2011 at 0:12
  • Updated context with actual plugin code sample in jsfiddle Commented Nov 20, 2011 at 0:34

3 Answers 3

1

Well, if this is plugin that uses jquery objects you still need to have jquery object

So you have to create element, but it is not mandatory to insert it into dom

$('<tmp>').append(yourTextVariable).yourPlugin()
Sign up to request clarification or add additional context in comments.

2 Comments

True enough. I originally thought the element was excess but it is straightforward.
It's much less complex to use a throw-away element than to create and maintain a method that doubles as a jQuery plugin and as a standard method.
1

Did you try something like this?

$('<div>' + stringToParse + '</div>').parseuri().port;

1 Comment

I had thought about this but considered it excess work making an element for the sake of it, however answers and discussion seem to indicate this might be better than going to the trouble of integrating a string-specific version.
1

I don't know how your plugin is written, but you should be able to call it with whatever argument you like:

$.fn.yourPlugin.call("someString");

However, chances are, as it's a jQuery plugin, it's expecting a jQuery object and not a String. You would have to provide more details if that's not what you're looking for, unless I've misunderstood your question.

Note that $.fn is just an alias for $.prototype.

In your plugin body you could test the type of this and do one thing if it's a String and another if it's not:

if(Object.prototype.toString.call(this) === "[object String]") {
    //It's a String!
}

1 Comment

See my edit. You could check whether this is a String or not in your plugin and differentiate appropriately. But I think the other solutions are much better (just create an element containing your string).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.