$location strips slashes from search parameters - javascript

I need a url with a query string like this:
/api/search/?path=parent/parent/parent/child
So I use $location like this:
$location.search('path', 'parent/parent/parent/child')
But it ends up with the delimiters stripped like:
/api/search/?path=parentparentparentchild
Is there a way to get the unescaped slashes without decoding them? I want to avoid:
?path=parent%2Fparent%2Fparent%2Fchild

Related

optional part for String.replace in javascript

I'm doing something like this:
ErjaView.ServiceListData.replace(`${detail.ID},${detail.Count}#`,"")
I just want to make the hashtag (#) optional.
I want to replace it if my string have one but if it doesn't, I still want to replace my string. how can I do this?
condition like this: replace the string if it does have such form. but if you couldn't find # do it anyways.

Forward slash in window.location.href redirects to root url

I'm generating window.location.href attributes where the path can sometimes include a slash "/".
ImageButton.Attributes.Add("OnClick", "window.location.href='DynamicItemDetail.aspx?Partno=" & strItemCode & "&Decorloc='")
The final strings look like this:
window.location.href="myurl.com/products.aspx?_Category=130&Partno=WWS-AWT/SWD&Decorloc="
Unfortunately, since the item code contains a slash, window.location redirects to the root url. Is there anyway to tell Javascript not to treat the slash as a subdirectory?
You will need to escape the characters in the URL.
encodeURIComponent is what you are looking for.
var encodedItemCode = encodeURIComponent(strItemCode);
ImageButton.Attributes.Add("OnClick", "window.location.href='DynamicItemDetail.aspx?Partno=" + encodedItemCode + "&Decorloc='")
The resulting URL will be
myurl.com/products.aspx?_Category=130&Partno=WWS-AWT%2FSWD&Decorloc=
Use the encodeURIComponent() Javascript function to encode the slash and any other special characters into a format that's allowed to be used in a URI.
A good reference for the function is here: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent
You need to escape the slash as %2F
Yes. You should URL encode the string, or the slash. So, instead of "/" you use "%2F"

Replace everything after last character in URL

I have the following code which replaces the current URL using JavaScript:
window.location.replace(window.location.href.replace(/\/?$/, '#/view-0'));
However if I have a URL like:
domain.com/#/test or domain.com/#/
It will append the #/view-0 to the current hash. What I want to is replace EVERYTHING after the last part of the URL including any query strings or hashes.
So presume my regex doesn't handle that... How can I amend it, to be more aggressive?
The following syntax may help:
location.href.replace(/[?#].*$/, '#/view')
It will replace everything after (and together with) ? or # in the string with #/view.
(^[^\/]*?\/)(?:.*)
Use this.Replace by \1 then your string
See demo.
http://regex101.com/r/sA7pZ0/28

Javascript regex to get src url between script tag

I wanted to get name of the script from such a string:
var text = '<script src="scripts/044c7c5e.vendor.js"></script><script src="scripts/fa9f85fb.scripts.js"></script>'
I wanted to retrieve the second script name i.e. fa9f85fb.scripts. How can I achieve this using javascript regex?
I'm writing something like this:
text.match(new RegExp(/<script src="scripts\/[(.*?)]\.scripts\.js"><\/script>/), 'g')[0]
But its returning the whole string.
Your pattern grabbing is a bit off; [(.*?)] should instead be (.*?) simply:
/<script src="scripts\/(.*?)\.scripts\.js"><\/script>/g
will be the entire regex, no need to call the RegExp class constructor either. The matched string is stored at index 0. The various segments are then stored from index 1 onwards.
text.match( /<script src="scripts\/(.*?)\.scripts\.js"><\/script>/g )[1]
Try /\w+.scripts(?=.js)/ ?
Reference: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions
Your match pattern is a bit vague. I can simply use /fa9f85fb.scripts/ to match it.

Need to escape the quotes of a variable in MVC View to pass as a parameter to JavaScript

I have a MVC view in which I have to pass a string variable to JavaScript, but that string variable has single quotes in it ('). I am trying to do something like this
<a onclick="JavaScript:AddressHandler.ProcessAddress('<%= homeAddress %>');"
class="button-link">change</a>
homeAddress has single quotes which I have to workaround somehow so that I can pass the complete value of it to the JavaScript.
You can use Ajax helper: Ajax.JavaScriptStringEncode(string strToEncode)
To escape a string to be a Javascript string literal, you replace backslash with double backslashes, and the string delimiter with a backslash and the delimiter:
<a onclick="AddressHandler.ProcessAddress('<%= homeAddress.Replace(#"\", #"\\").Replace("'", #"\'") %>');" class="button-link">change</a>
Note: The javascript: protocol is used when you put script in an URL, not as an event handler.
Edit:
If the script also contains characters that need HTML encoding, that should be done after escaping the Javascript string:
<a onclick="<%= Html.Encode("AddressHandler.ProcessAddress('" + homeAddress.Replace(#"\", #"\\").Replace("'", #"\'") +"');") %>" class="button-link">change</a>
So, if you don't know what the string contains, to be safe you need to first escape the string literal, then HTML encode the code so that it can be put in the attribute of the HTML tag.
You can write a method that escapes all single quotes (and other characters if needed) with a backslash so it is not misunderstood by javascript.
You'll want to encode homeAddress as a URL. MVC has a built in helper to do this: UrlHelper.Encode(string url) - it should replace a single quote with %27
I don't have time to test it, but look at HtmlHelper.Encode(string s). It might handle the escaping for you.

Categories