I'm using substring(1) to get the last character off of an href attribute.
I'm talking javascript. On my development site, everything works great and the href http://www.mysite.come/page/#B returns B and all is well.
On the live version of the site, the same substring(1) on the same href returns ttp://www.mysite.com/page/#B - it takes off the first character and returns the rest.
I have no idea why it does this. I've used this same script on multiple sites with no issues. The only thing possibly related is that the dev site is running a newer version of jquery (1.8.1) and the live is on 1.7.1.
I used substr(-1,1) instead and everything works fine, but I'd like to know if someone can tell me why I got 2 different results from the same input.
Thanks!
Extra Info
I've been using the OrganicTabs script from CSS Tricks for a while now (http://css-tricks.com/organic-tabs/) and never had a problem with it until recently. I tracked it down to the substring line:
var curList = base.$el.find("a.current").attr("href").substring(1)
An example of one of the links I'm this script is targeting:
I have modified the script to use substr instead, but I don't know why I would get inconsistent results with substring.
The href you are reading locally, is storing only the hash part of the url,
while for some reason the Live website href's have the whole url
Link vs Link
The substring function is working as expected, since substring(1) should return everything but the first character.
Check how you generate your urls
EDIT: The best solution is to get the substring starting from the hash (excluding it), which covers both cases, as well as cases where the hash is longer than 1 character
href.substring(href.indexOf("#")+1)
String.prototype.substring(n [,...]) returns the first n characters from the string if n is positive, not the last. You need substr(-1) for this.
See:
MDN: substr vs. substring
Related
I have a big string (1116902 char length) that I want to process with a regex (pretty simple one). I get a response from a soap server that is encoded in base64. So I just get the result between the appropriate xml tags and then decode the response.
This working for a small request. But when I get a big response back, the callback function of the replace() method is never called. I have tried to test the string on the regex101 website and it can find the result. So I wonder if there is a limitation in my JavaScript engine. I'm working on a Wakanda Server V10 that use Webkit as JavaScript engine. I cannot provide the string because it contains some enterprise information.
Here is my regex : /xsd:base64Binary">((.|\n)*?)<\/responseData>/
I taught it is maybe a special character that is not included in the ((.|\n)*?) group. But then why the regex101 find out the result (then maybe is the JavaScript engine)
Maybe anybody can help me?
Thanks
If you can guarantee that there are no tags between your start and end delimiter, which sounds like it might be the case, you could just change your RE to
/xsd:base64Binary">([^<]*)<\/responseData>/
which shouldn't require any backtracking and might work for you.
[^<] simply means everything but the < character. Since there shouldn't be any tags between the open and closing tags of your section (at least that's what I understand) that will accept everything until you hit your closing tag. The important thing is that the RE engine can tell immediately whether something matches that or not, so no branching or backtracking is required.
I have several html A tag generated programmatically in ASP.NET with a JavaScript function taking long parameter in href. One of those has over 20K characters when it get assigned in backend, but I am seeing the actual link has only 5239 characters on the browser side and the JavaScript function does not have closing. So the link never works. I am thinking about workarounds for this implementation since it's not a good idea to put this much amount of data in links, but now I'm just curious about cause of the issue.
Examples of the code assigning values to the link:
HtmlAnchor.HRef = "javascript:doSomething('Import','" + strHeader_LineIds + "');"
In this case the variable strHeader_LineIds carries a string over 20k characters.
Example of what I'm actually seeing in client side:
<a id=anchor1 class=class1 href="javascript:doSomething('Import', 'blahblahblahblah....">Link Text</a>
Please note the javascript function has no closing here. But when I'm debugging in backend I do see the closing of the function.
I guess this issue may have something to do with the browser's URL limit? I am using IE and I learned IE has a maximum URL length limit as 2,083 characters from Here. But how can the link show up with 5,239 characters?
I've had a similar issue with javascript like dynamic functions created in code and then called. I found that I had to play with swapping out single quotes in the javascript function with double quotes or escaping the quotes.
Then again just reading your post could be a limit issue.
Have you tried assigning the long to an element in the background and then referencing that as part of the javacript. I know IE gets funny with spaces in passed in parameters.
I think found an answer to the issue though. According to This Article:
JavaScript URIs
The JavaScript protocol is used for bookmarklets (aka favlets), a lightweight form of extensibility that permits a user to click a button and run some stored JavaScript on the currently loaded page. In IE9, the team did some work to relax the length limit (from ~260 characters, if I recall correctly) to something significantly larger (~5kb, if I recall correctly).
So I just hit the ~5kb limit.
I'm using this technique to extract the click events in my SharePoint site. It uses jquery and a regular expression to capture clicks and report them as events to google analytics.
I'm also just past total newbie with regex -- It is starting to make some sense to me, but I have a lot to learn still. So here goes.
I have a preapproved list of filetypes that I am interested in based on the site listed above.
var filetypes = /\.(zip|pdf|doc.*|xls.*|ppt.*|mp3|txt|wma|mov|avi|wmv|flv|wav|jpg)$/i;
But it isn't quite working like I need. With the $ I assume it is trying to match to the end of the line. But often in SharePoint we get links like this:
example.org/sharepoint/_layouts/15/wopiframe.aspx?sourcedoc=/sharepointlibrary/the%20document%20name.docx&action=default&defaultitemopen=1
The two problems I have are, I can't count on the file name being before the query or hash and I can't count on it being at the end. And all the different Microsoft Office extensions.
I found this thread on extracting extensions, but it doesn't seem to work correctly.
I've put together this version
var filetypes = \.(zip|pdf|doc|xls|ppt|mp3|txt|wma|mov|avi|wmv|flv|wav|jpg)[A-Za-z]*
I changed the office bits from doc.* to just plain doc and added the optional alpha character afterwards. And removed the $ end anchor. It seems to be working with my test sample, but I don't know if there are gotchas that I don't understand.
Does this seem like a good solution or is there a better way to get a predetermined list of extensions (including for example the Office varions like doc, docx, docm) that is either before the query string or might be one parameter in the query string?
I would go with the following which matches file name and extension:
/[^/]+\.(zip|pdf|doc[xm]?|xlsx?|ppt|mp3|txt|wma|mov|avi|wmv|flv|wav|jpg)/i
Outputs the%20document%20name.docx from you example.
There may be other formats that it might not work on but should get you what you want.
I have an app that gets youtube video id's to share them out over twitter. Looking at the tweets I can see users are sharing them out, but in some cases the share content is broken.
We have a restful API which responds with the id, and then with a string replace method we replace {{id}}.
http://twitter.com/intent/tweet?text=http://www.youtube.com/watch?v={{id}} Share your video to win.
Replace method
var href = $('#twitter-btn').attr('href');
$('#twitter-btn').attr('href', href.replace('{{id}}', response.youtube_id) );
In most cases the tweets can be seen like so...
http://www.youtube.com/watch?v=0bxW5fd Share your video to win.
But in some cases we are seeing...
http://www.youtube.com/watch?v
What I find really strange is that the '=' has been removed as well. We have ruled out the server giving a bad response as we are logging all ID's to check they are valid.
Is anyone aware of how this could happen, or if certain ID's break twitter as I am aware the '#' symbol needs to be url encoded to %23 else it breaks.
The share functionality works using the standard twitter share.
<script type="text/javascript" async src="//platform.twitter.com/widgets.js"></script>
This has really stumped me as it is such a basic task I cant understand how this can happen.
Any Ideas would be very appreciated.
replace isn't broken. If the href string really contains
http://twitter.com/intent/tweet?text=http://www.youtube.com/watch?v={{id}} Share your video to win
...then your replace as shown will replace the {{id}} with the value from response.youtube_id.
Given that the = isn't showing up in the result, the only possibilities I see are:
href doesn't have the ={{id}} in it in the first place. Perhaps something is modifying the attribute before you grab it.
You're not looking directly at the result of the replace, but at something derived from it, and in the process of the derivation the = and the value after it have been removed, perhaps only when the = was originally followed by something invalid (possibly blank) — in which case, the problem would be with the response.youtube_id.
But don't focus on the replace, it's not the problem.
http://www.dummies.com/how-to/content/how-to-insert-links-into-tweets.html
or use http encoding for querystring parametter text=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D%7B%7Byourid%7D%7D
and
$('#twitter-btn').attr('href', href.replace('yourid', response.youtube_id) );
this should be it.
So i've got a regex that identifies URLs:
/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/
But when I use it to identify urls that a user has entered, simply using .test slows the page down considerably, even though according to the MDN, it's supposed to be faster than exec. Am I using an outdated method of testing Regular Expressions? Is there a faster method that i don't know about? or is my regex just really long and complicated?
Here's a JSFiddle.
Edit:
Takes 20.7 seconds in Chrome, v24
1:48.5 in Internet Explorer 9
So it seems that the regex only lags when it processes a url that has posted information, for example in the jsfiddle url Product.aspx?Item=N82E16811139009. When that part of the url is removed, the regex preforms correctly, and quickly.
However, removing the last star from ([\/\w \.-]*)* makes the regex preform incorrectly, so using ([\/\w \.-]*) is not an option.
Rather, for the regex to be able to handle urls with posted information, the last part needs to be removed:
/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/
to
/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*/
this is because the regex is designed to detect filetypes or a backslash at the end of the url, not a question mark and posted information. removing the last part fixes the problem and the regex runs correctly and quickly.