Monday, July 27, 2009

Visual Studio Macro: Wrap Selected Text in Tag

This simple macro allows you to wrap whatever text is selected in a tag - a feature that somehow is missing from Visual Studio 2008...

    Sub SurroundWithTag()
        Dim selectedText = DTE.ActiveDocument.Selection.Text
        Dim tag As String
        tag = InputBox("Enter tag name", "Tag", "strong")
        If (tag = "") Then
            MsgBox("No tag defined")
        Else
            ' no end tag needed as Visual Studio creates the end tag..
            DTE.ActiveDocument.Selection.Text = String.Format("<{0}>{1}", tag, selectedText)
        End If
    End Sub

Based on the answer to this question found on Stack Overflow: Macro to wrap selected text with tags in Visual Studio (which also details how you can create a macro)

Wednesday, July 08, 2009

jQuery Quick Tip: Extract CSS Background Image

jQuery allows you to get the background image of any element on a web page:

$("#myelement").css("background-image");

However, this returns it in an undesirable format: url(http://example.com/images/image.jpg) or url("http://example.com/images/image.jpg"). With a bit of string replacement, you can get extract the URL:

function extractUrl(input)
{
 // remove quotes and wrapping url()
 return input.replace(/"/g,"").replace(/url\(|\)$/ig, "");
}

So now you can just do this:

extractUrl($("#myelement").css("background-image"))

Which will return the URL on its own http://example.com/images/image.jpg.