Remove (#) from URL in Silverlight - javascript

I have a URL e.g. http://localhost:8000/#Test/Method
I am doing following:
System.Windows.Browser.HtmlPage.Window.CurrentBookmark = string.Empty;
which just remove Test/Method but not '#'
I need to modify browser URL
as:
http://localhost:8000/
Any ideas on how to fix this?

If you are manipulating a String you can use the following method :
String Url = "Foo#Bar";
Url = Url.Replace("#", string.Empty);

Use regular expression
String url = "http://localhost:8000/#Test/Method"
url = url.replace(/#/g, "");
The above reg expression will scan for all occurrences of '#' character and replaces with empty character

Related

Decode url string in javascript give empty string

I have this string:
http:\/\/www.emptyurl.com\/
And i want to change the url to :
http://www.emptyurl.com/
With :
url = url.replace(/\\//gi, "/");
But it give me empty string.
Any idea how i can fix it?
You can use decodeURIComponent()
var url = 'http:\/\/www.emptyurl.com\/';
url = decodeURIComponent(url);
console.log(url);

Way to replace substring that in regular expression with another

I want to know way to replace substring in url with new string.
https://pbs.twimg.com/media/DW7hPt9VAAAdKE7?format=jpg&name=small
after "&name=" they are many kind of size like
900x900,medium,360x360,small
let href = document.location.href;
if(!href.includes('&name=orig')){
if(href.includes(/900x900|medium|360x360|small/){ //if href have some size in regular expression
// I try to make it search for substring in regular expression
document.location.href = href.replace(/900x900|medium|360x360|small/,'orig');
}
else{ //if url don't have '&name=' like above
var adding = '&name=orig';
document.location.href = link+adding;
}
}
It not working
I don't want to write code to check all case like
if(href.includes('900x900')
if(href.includes('medium')
if(href.includes('360x360')
if(href.includes('small')
they are way to find match at once?
change if(href.includes(/900x900|medium|360x360|small/){ to
if(href.search(/900x900|medium|360x360|small/) !== -1){
as includes accepts the string not the regex.

Remove part of url using javascript

I need to remove part of url if it matches //videoid?v=
assuming my url can be
- www.abc.com/video/
- www.abc.com/video/videoid?v=1234567
- www.abc.com/video//videoid?v=1234567
in case url has // forward slash before videoid?v= then i need to remove single / from the url so that url will be correct such as www.abc.com/video//videoid?v=1234567
currentURL = document.URL;
You can use regex and use it like this to remove double occurrences of "/":
"www.abc.com/video//videoid?v=1234567".replace(/([^:]\/)\/+/g, "$1");
Working example: https://jsfiddle.net/os5yypqm/3/
EDIT:
I edited the JSFiddle to include "http://" in front of the url so that you can see that this does not affect it (and also read it here). Can't use it on SO though so you need to see the fiddle.
If your only concern is the double slash.
var currentUrl = 'www.abc.com/video//videoid?v=1234567';
currentUrl.split('//').join('/');
Results in www.abc.com/video/videoid?v=12345678
You can replace forward slash if followed by forward slash and "v" with empty string
"www.abc.com/video//videoid?v=1234567".replace(/\/(?=\/v)/g, "")

How to ignore spaces in url parameter with javascript

I want to ignore spaces in a url parameters. Currently here is my line of script that gets the url parameter of a link.
var paramValue = href.split.('=')[1];
I want to modify it so it ignores any spaces found in the url parameter
Just remove the spaces.
var paramValue = href.split('=')[1].replace(/ /g, '');

javascript regular expression replacement doesnt work?

why this regular expression replacement doesnt work?
var url = 'http://myweb.com/page/1/id/2';
newUrl = url.replace('/page\/[0-9]+/', 'page/2'); //it must become http://myweb.com/page/2/id/2
You need to do two things:
change str.replace to url.replace
remove the ' around the regex
var url = 'http://myweb.com/page/1/id/2';
newUrl = url.replace(/page\/[0-9]+/, 'page/2');
Example: http://jsfiddle.net/fhqXn/
Use url rather than str, if you want to replace something in the String stored in the url variable.
You have a naming misspell.
Rename your url var to str or change the str.replace to url.replace:
newUrl = url.replace('/page\/[0-9]+/', 'page/2');

Categories