jQuery: how to get URL and modify it? [closed] - javascript

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a link:
http://blog.isvn.pl/search/music
How can I modify this link with jQuery to get:
http://blog.isvn.pl/tagged/music
I want change only „search“ to „tagged“, and open new link.

You can use replace()
var url = 'http://blog.isvn.pl/search/music';
var newUrl = url.replace('/search/', '/tagged/');
window.location.href = newUrl;
//or, to open in a new window:
window.open(url,'_blank');
Demo

You can use
var value = 'http://blog.isvn.pl/search/music';
value = value.replace("search", "tagged");
alert(value);

use the replace function in javascript:
var currentUrl = "http://blog.isvn.pl/search/music";
var replacedUrl = currentUrl.replace("/search/", "/tagged/");
I use /search/ (with the slashes) so it only replaces when it is a folder in the path. Otherwise it would also replace when the word search in in a song title.

You can use replace(), to replace search with tagged
Live Demo
url = url.replace('search', 'tagged');
To change the url
window.location.href = window.location.href.replace('search', 'tagged');
OR
window.location.href = document.URL.replace('search', 'tagged');

Related

add a variable in href using javascript [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I am new in javascript i want to add a javascript variable in
i use some javascript like this but it not working
HTML
<ul id="direction"></ul>`
JS
var abc = 'link/';
var cba = 'hello';
document.getElementById('directions').innerHTML = 'Link';`
This code should work, make sure the html id matches the getElementById and use the right quotes as mentioned before.
You want to add a href to a list but you should add a li first, in my example I use a div tag.
var name = 'google';
var ext = '.com';
document.getElementById('direction').innerHTML = 'Link';
A better way to do this would be by using backticks `` < these like so:
var name = 'google';
var ext = '.com';
var link = 'Link';
document.getElementById('direction').innerHTML = `${link}`;
Your code, fixed ID, and quotes:
document.getElementById("direction").innerHTML='Link'
Or maybe better:
var node=document.createElement("A")
node.setAttribute("href","https://"+var1+var2)
document.getElementById("direction").appendChild(node)
I hope that this will help you!
I'd recommend avoiding using innerHTML in most cases! The following accomplishes what you want programmatically without needing to generate HTML strings:
window.onload = () => {
let abc = 'link/';
let cba = 'hello';
let container = document.getElementById('direction');
let link = container.appendChild(document.createElement('a'));
link.setAttribute('href', `https://${abc}${cba}`);
link.appendChild(document.createTextNode('Link'));
};
<ul id="direction"></ul>

Get the value of a div [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Helllo, I'm having a div like this:
<div>1+1</div>
How can I get these '1+1' to the javascript and calculate them to '2'?
Thanks in advance!
you can use innerHTML property of element by javascript to extract the value:
document.getElementById("div").innerHTML
and then use eval() method to evaluate the math expression:
var exp=document.getElementById("div").innerHTML;
alert(eval(exp));
You need to somehow identify this div, e.g
<div id="div-with-expression">1+1</div>
And code will be like:
console.log(eval(document.getElementById('div-with-expression').innerText));
You can use eval:
var equation = getElementById(IdOfDiv).innerHTML;
var result = eval(equation);
Well, You could use the Javascript eval() func:
http://www.w3schools.com/jsref/jsref_eval.asp
But, eval functions can introduce malicious code into your environment.
I'm not sure as to what your purpose to start with and what your assumptions are for that input but here's another way to go about it with regex although it's more complicated:
var myRegexp = /(\d+)(\+)(\d+)/;
var match = myRegexp.exec("1+1");
// parseInt(match[0]) == 1 ,as an Int
// match[1] = '+', you could figure what operand this is with 'if else' conditions
// parseInt(match[2]) == 1 ,as an Int

Why does the Javascript replace() function not do anything? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Here's my function:
CSS_Left = '450px';
CSS_Left.replace('px', '');
alert(CSS_Left);
It returns '450px'
Here's an example:
http://jsfiddle.net/D2Mgb/
I want it to replace 'px' with nothing.
In JavaScript, Strings are immutable.
All types except objects define immutable values. Specifically, strings are immutable (unlike in C for instance).
So, str.replace returns a new string. Try this instead:
CSS_Left = CSS_Left.replace('px', '');
Or if you don't want to overwrite the original string, just do it in the alert call
alert(CSS_Left.replace('px', ''));
It's worth noting that .replace can also take a regexp. Though it's not necessary in your case, you could do something like this to achieve the same result
CSS_Left = CSS_left.replace(/px$/, '');
Because .replace() return a new string. If you want to update the old string you should write:
CSS_Left = CSS_Left.replace('px', '');
I am not sure, but the function replace() creates a new variable.
Try this :
var CSS_Left = '450px';
CSS_Left = CSS_Left.replace('px', '');
alert(CSS_Left);
The output of the replace call needs to be reassigned to CSS_Left

Pass variable from Parent Window Javascript Function to Popup Window Div [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How do I pass the variable "appName" from the SerialPopUp function to the pop up window I create? Thank you in advance for any help you can give me.
function SerialPopUp(appInfo)
{
appInfo=appInfo.split("\n");
var appName = appInfo[1].trim();
var appType = appInfo[5].trim();
var url = appInfo[7].trim();
url = appInfo[7].substring(9,appInfo[7].indexOf(">")-17);
var serialNum = "";
if(appType == 'External Paid'){
x = window.open(url, "Get Serial Number","resizable=1,status=1, width=700, height=250");
x.document.getElementById("appName").innerHTML= appName;
}
}
try
if(appType == 'External Paid'){
url = url + '?appName=' + appName;
x = window.open(url, "Get Serial Number","resizable=1,status=1, width=700, height=250");
x.document.getElementById("appName").innerHTML= appName;
}
This will pass the variable to the pop up window in the url.
Use a query string variable. Extra text here.

Adding an id on to a href jquery [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm trying to get the id of an radiobutton to be added to href when the link is clicked. I tried searching google for a long time. But maybe i'm missing because it's very basic.
I do have a bit of code written but it doesn't work. Help?
function ProductIDLink() {
var url = "/bbbb/aaaaaaa/" + ('.aaasaff:radio:checked').attr('id');
$("#safad").attr("href", url)
}
Looks like you've just forgotten a $:
... ('.aaasaff:radio:checked').attr('id')
should be
... $('.aaasaff:radio:checked').attr('id')
From what I can see the $ was missing.
function ProductIDLink() {
var url = "/bbbb/aaaaaaa/" + $('.aaasaff:radio:checked').attr('id');
$("#safad").attr("href", url)
}

Categories

Resources