/    Sign up×
Community /Pin to ProfileBookmark

Modify Link css using JS

Hi everyone, I’m using this code to create an interaction beetween JS and CSS

[CODE]document.styleSheets[/CODE]

What I need to do is to retrieve the original href attribute of the css (<link href=”desired_url” …) and to change it from a relative path to an absolute one.

I succesfull retrieved the originale url using

[CODE]document.styleSheets[i].href[/CODE]

but when i try to set the new url (after my own changes) with this code

[CODE]document.styleSheets[i].setAttribute=(‘href’, newurl); [/CODE]

I can’t see the result I expected neither I got any error! (I use firebug to detect code’s errors)

The url simply does not change (I check it with an alert located after the code)…any idea?

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@FangSep 24.2007 — document.getElementsByTagName('link')[i].setAttribute=('href', newurl)
[I]styleSheets[/I] method is for accessing the contents of the css

See also: http://www.alistapart.com/stories/alternate/
Copy linkTweet thisAlerts:
@embraceauthorSep 24.2007 — Thanks a lot I was completely wrong trying to set that attribute...i read the page you linked me but i got no result, also trying your code directly. Here's my code

<i>
</i>for (w=0;w&lt;document.styleSheets.length;w++)
{
ssheet=''+document.styleSheets[w].href+'';
//Various operations on the string
newurl='http://'+dominio+'/'+ssheet;
alert(newurl);
document.getElementsByTagName('link')[w].setAttribute=('href', newurl)
alert(document.styleSheets[w].href);
}


The variable newurl contains exactly what i need but the document.styleSheets[w].href does not change at all...Am I doing anything wrong?
Copy linkTweet thisAlerts:
@FangSep 24.2007 — [I]styleSheets[/I] method is for accessing the [B]contents[/B] of the css, i.e. rules, properties or values.
for (w=0;w&lt;document.getElementsByTagName('link').length;w++)
{
//Various operations on the string
newurl='http://'+dominio+'/'+ssheet;
alert(newurl);
document.getElementsByTagName('link')[w].setAttribute('href', newurl)
}
Copy linkTweet thisAlerts:
@embraceauthorSep 24.2007 — Even with the following code I got no result...

[CODE]
for (w=0;w<document.getElementsByTagName('link').length;w++)
{
ssheet=''+document.getElementsByTagName('link')[w].href+'';
newurl='http://'+dominio+'/'+ssheet;
alert(newurl);
document.getElementsByTagName('link')[w].setAttribute=('href', newurl);
alert(document.getElementsByTagName('link')[w].href);
}
[/CODE]


the final alert did not change
Copy linkTweet thisAlerts:
@FangSep 24.2007 — No '='document.getElementsByTagName('link')[w].setAttribute[COLOR="Red"]=[/COLOR]('href', newurl);
×

Success!

Help @embrace spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...